Detect the Internet connection is offline?

  1. Home
  2. javascript
  3. Detect the Internet connection is offline?

How to detect the Internet connection is offline in JavaScript?

First answer

You can determine that the connection is lost by making failed XHR requests.

The standard approach is to retry the request a few times. If it doesn’t go through, alert the user to check the connection, and fail gracefully.

Sidenote: To put the entire application in an “offline” state may lead to a lot of error-prone work of handling state.. wireless connections may come and go, etc. So your best bet may be to just fail gracefully, preserve the data, and alert the user.. allowing them to eventually fix the connection problem if there is one, and to continue using your app with a fair amount of forgiveness.

Sidenote: You could check a reliable site like google for connectivity, but this may not be entirely useful as just trying to make your own request, because while Google may be available, your own application may not be, and you’re still going to have to handle your own connection problem. Trying to send a ping to google would be a good way to confirm that the internet connection itself is down, so if that information is useful to you, then it might be worth the trouble.

Sidenote: Sending a Ping could be achieved in the same way that you would make any kind of two-way ajax request, but sending a ping to google, in this case, would pose some challenges. First, we’d have the same cross-domain issues that are typically encountered in making Ajax communications. One option is to set up a server-side proxy, wherein we actually ping google (or whatever site), and return the results of the ping to the app. This is a catch-22 because if the internet connection is actually the problem, we won’t be able to get to the server, and if the connection problem is only on our own domain, we won’t be able to tell the difference. Other cross-domain techniques could be tried, for example, embedding an iframe in your page which points to, and then polling the iframe for success/failure (examine the contents, etc). Embedding an image may not really tell us anything, because we need a useful response from the communication mechanism in order to draw a good conclusion about what’s going on. So again, determining the state of the internet connection as a whole may be more trouble than it’s worth. You’ll have to weight these options out for your specific app.

Second answer

There are a number of ways to do this:

  • AJAX request to your own website. If that request fails, there’s a good chance it’s the connection at fault. The JQuery documentation has a section on handling failed AJAX requests. Beware of the Same Origin Policy when doing this, which may stop you from accessing sites outside your domain.
  • You could put an onerror in an img, like

    <img src='' 
          onerror='alert("Connection dead");' />

    This method could also fail if the source image is moved / renamed, and would generally be an inferior choice to the ajax option.

So there are several different ways to try and detect this, none perfect, but in the absence of the ability to jump out of the browser sandbox and access the user’s net connection status directly, they seem to be the best options.

Third answer

 } else {
Spread the love

Related articles

Comments are closed.