Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

navigator.onLine not always working

Tags:

javascript

I'm having an issue with the navigator.onLine property.

I'm running a simple website from a local kiosk running on a WAMP.

On my laptop when I test this it works. I turn off WiFi and the alert box shows up. Disconnecting the internet on the kiosk running the WAMP software does not produce the false status. Any ideas why?

var online = navigator.onLine;  if (online == false) {      alert("Sorry, we currently do not have Internet access.");     location.reload();  } 
like image 387
Jako Avatar asked Jan 11 '13 17:01

Jako


People also ask

Which is used to check the online status of the browser?

onLine. Returns the online status of the browser.

How do I know if JavaScript is connected to Internet?

JavaScript has a function called navigator. This browser property can be used to determine if you are connected to the internet. It returns true or false depending on the status of the client's network and says whether the browser is online or offline easily.

What is Window navigator?

The navigator property of a window (i.e. window. navigator ) is a reference to a Navigator object; it is a read-only property which contains information about the user's browser. Since Window is a global object and it is at the top of the scope chain, so properties of the Window object such as window.


2 Answers

MDN about navigator.onLine:

In Chrome and Safari, if the browser is not able to connect to a local area network (LAN) or a router, it is offline; all other conditions return true. So while you can assume that the browser is offline when it returns a false value, you cannot assume that a true value necessarily means that the browser can access the internet.

As described above, this property is not trustable, so, in my opinion, the best workaround is an ajax call to a server-side page. If the browser is offline, then the connection will fail and, thus, the onerror event will be called. Otherwise, the onload event is called:

function isOnline(no,yes){     var xhr = XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHttp');     xhr.onload = function(){         if(yes instanceof Function){             yes();         }     }     xhr.onerror = function(){         if(no instanceof Function){             no();         }     }     xhr.open("GET","anypage.php",true);     xhr.send(); }  isOnline(     function(){         alert("Sorry, we currently do not have Internet access.");     },     function(){         alert("Succesfully connected!");     } ); 
like image 103
Danilo Valente Avatar answered Sep 25 '22 10:09

Danilo Valente


If you are using axios:

axios.request(options).catch(function(error) {   if (!error.response) {     // network error (server is down or no internet)   } else {     // http status code     const code = error.response.status     // data from server while error     const response = error.response.data   } }); 
like image 21
Ahmad Mobaraki Avatar answered Sep 24 '22 10:09

Ahmad Mobaraki