javascript - Detect that the Internet connection is offline? -
how detect internet connection offline in javascript?
you can determine connection lost making failed xhr requests.
the standard approach retry request few times. if doesn't go through, alert user check connection, , fail gracefully.
sidenote: put entire application in "offline" state may lead lot of error-prone work of handling state.. wireless connections may come , go, etc. best bet may fail gracefully, preserve data, , alert user.. allowing them fix connection problem if there one, , continue using app fair amount of forgiveness.
sidenote: check reliable site google connectivity, may not entirely useful trying make own request, because while google may available, own application may not be, , you're still going have handle own connection problem. trying send ping google way confirm internet connection down, if information useful you, might worth trouble.
sidenote: sending ping achieved in same way make kind of two-way ajax request, sending ping google in case pose challenges. first, we'd have same cross-domain issues typically encountered in making ajax communications. 1 option set server-side proxy, wherein ping
google (or whatever site), , return results of ping app.. catch-22, because if internet connection problem, won't able server, , if connection problem on our own domain, won't able tell difference. other cross-domain techniques tried, example, embedding iframe in page points google.com, , polling iframe success/failure (examine contents, etc). embedding image may not tell anything, because need useful response communication mechanism in order draw conclusion what's going on. again, determining state of internet connection whole may more trouble it's worth. you'll have weight these options out specific app.
Comments
Post a Comment