12

How to check if there is an internet connection in Javascript? I have searched in Google but I have not found a good solution for my problem.

I have already got two eventhandler:

document.body.addEventListener("offline", function () {
    //alert("offline")
    masterViewModel.online(false)
}, false);
document.body.addEventListener("online", function () {
    //alert("online")
    masterViewModel.online(true);
}, false);

But how to check in the "onload"-function if I am online?

5
  • it is the work of browser so let it be Commented Apr 16, 2013 at 11:15
  • 1
    If the page is being loaded, aren't you already online?? Commented Apr 16, 2013 at 11:15
  • 1
    Well, nope, you can open page from local server or even from file system. The only thing I can suggest is to create lightweight ajax request and timeout counter. If timeout fired, connection probably broken.
    – Tommi
    Commented Apr 16, 2013 at 11:17
  • 1
    Check stackoverflow.com/questions/2384167/…
    – niksvp
    Commented Apr 16, 2013 at 11:21
  • This is actually working,, dont change your code... Commented Jun 19, 2022 at 13:12

4 Answers 4

30

In HTML5, according to the specs:

var online = navigator.onLine;
0
3

Hi You can do like this:

var connectionMessage = "internet connection";
var noConnectionMessage = "No internet connection.";
window.onload = checkInternetConnection;
function checkInternetConnection() {
  var isOnLine = navigator.onLine;
   if (isOnLine) {
      alert(connectionMessage);
   } else {
     alert(noConnectionMessage);
   }
}
1
  • 1
    I love how you change both messages just to correct a grammar problem xD Commented Apr 16, 2013 at 11:27
3

var online = navigator.onLine; is doesn't work every time.Sometime your connection is enabled but you can not fetch data from internet or not access a site even if you are online...so that time online return true because you are online.that time "navigator.onLine" is not usable.

" You could be getting false positives, such as in cases where the computer is running a virtualization software that has virtual ethernet adapters that are always "connected." Therefore, if you really want to determine the online status of the browser, you should develop additional means for checking. To learn more, see the HTML5 Rocks article, " check this article

you must use ajax request to check....

0

Why not just make an ajax call to a well-known address, say StackOverflow. If you don't get 404, you are online.

3
  • 1
    If there is no internet or the server is down, there is no 404. Commented Nov 9, 2017 at 12:58
  • 1
    404 is an actual response from a server, thus, you need to be online to get a 404.
    – tomacco
    Commented Feb 22, 2018 at 8:58
  • A 4xx response comes from the server, you won't get them when having no connection. You could also get a false positive if the site you're targeting is down.
    – Enric A.
    Commented Jul 29, 2020 at 9:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.