1

I want a simply code, that checks the internet connection inside the browser and a handling that handles the various codes, that will come back.

I already tried a http-get-request to handle the returning status code, but at the end, I had some issues with the same-origin-policy.

I also tried the ononline- and onoffline-event, but these are depricated and do not work in my target-browser.

My 3rd option was with navigator.onLine. When the Browser got Internet Connection, the output is true. But, when my browser got not connection, the answer of navigator.onLine is not false...

My main use case is to check, if the browser got internet connection or not. If he got Internet Connection, then he should open www.abc.de But if there is no Internet Connection, he should open a local application with localhost:8080/abc.

3 Answers 3

3

In principle this is a duplicate of Why navigator.onLine is inconsistent? Is there any reliable solutions?

However I would personally try something like

if (location.host.indexOf("localhost")==-1) { // we are not already on localhost
  var img = new Image();
  img.onerror=function() { location.replace("http://localhost:8080/abc"); }
  img.src="http://servertotest.com/favicon.ico?rnd="+new Date().getTime();
}
3
  • that is a nice thing. Thanks for the info
    – Sagar V
    Commented Mar 1, 2017 at 8:46
  • this works fine for me. When there is no Internet Connection, the wanted offline-Page is loading. BUT: How can I implement, that on a successfull load the other page is loading? onload is the wrong option, cause its true at every refresh Commented Mar 1, 2017 at 9:24
  • You want to load "servertotest.com/abc" if the server is up? You could use img.onload=function() { if (location.host.indexOf("servertotest")==-1) location.replace("http://servertotest.com/abc") } which will redirect from localhost or file system to the url on the server
    – mplungjan
    Commented Mar 1, 2017 at 9:30
1

To detect the offline and online status you need to use the below example.

Code Sample

// add "is-offline" class to the html element if user is offline and remove when online again
window.addEventListener('load', function() {
  function updateOnlineStatus(event) {
    if (navigator.onLine) {
      document.documentElement.classList.remove('is-offline');
      document.querySelector('.connection-status').innerHTML = 'Online';
    } else {
      document.documentElement.classList.add('is-offline');
      document.querySelector('.connection-status').innerHTML = 'Offline';
    }
  }

  window.addEventListener('online', updateOnlineStatus);
  window.addEventListener('offline', updateOnlineStatus);
});
.connection-status {
  font-size: 19px;
  font-weight: 600;
  color: #58b05c;
}

.is-offline .connection-status {
  color: red;
}
<center>
  <p class="connection-status">Online</p>
</center>

1
  • Please note, that according to MDN, these events do not represent the total truth. There are cases (like virtual networks) where the browser will report itself as being 'online' when the internet cannot be reached at all.
    – Drak
    Commented Jul 22, 2022 at 5:14
0

try to use this

window.navigator.onLine;

for more information, please refer

https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOnLine/onLine

0

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