0

i'm trying to detect whether the browser is connected to the internet or not using javascript. my implementation works for me on both Edge and chrome but does not works when i try to test in on another pc.

i tried clearing cache, cookie and even reinstall my browsers thinking it could be cache related but still it works on my machine but not when i try it on any other. the issue is strictly related to chrome as edge seems to work fine on any other pc.

   <!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" id="viewport" content="width=device-width,height=device-height,initial-scale=1.0,user-scalable=no">
    <script type="text/javascript">

       // Checking connectivity - Displays message and play sounds if lose
window.addEventListener('offline', () => {

console.log("offline");

});
window.addEventListener('online', () => {
console.log("online");

});


    </script>
    <title>Insert title here</title>
  </head>
  <body>


  </body>
</html>

i wrote the above test html, which again works on chrome on my machine but does not for others. the expected result should be print offline in console when you disconnect the pc from internet and online when reconnected without the need to refresh the page.

Emotional_Goose
  • 167
  • 1
  • 13
  • Try to wrap those window.addEventListener calls with additional load event. Also see [this](https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOnLine/Online_and_offline_events) about the usage and [this](https://developer.mozilla.org/en-US/docs/Web/API/Window/online_event) where compatibility is confirmed, if you were wondering. – Mike Sar Aug 14 '19 at 09:35
  • 1
    Instead of unplugging your connection go to F12 -> network -> OFFLINE works like a charm – Raimonds Aug 14 '19 at 09:40
  • @MikeSar thanks for the feedback, i tried that but still no luck. weirdly it works on my machine but not when i try it on another pc. browsers version are the and the setting are identical – Emotional_Goose Aug 14 '19 at 09:46
  • @Raimonds i see, but the whole point is to be able to detect it in real life. when the wifi connection drop and the browser is not longer connected. this event should fire. – Emotional_Goose Aug 14 '19 at 09:47
  • https://stackoverflow.com/questions/3181080/how-to-detect-online-offline-event-cross-browser. This is a related post. – Sergio Rodriguez Aug 14 '19 at 09:51
  • 1
    In this case I think most reliable way is to set up some sort of long polling with server. – Raimonds Aug 14 '19 at 10:00
  • 1
    Could you give som more details about the specs of the other pc you tested on? I tested your page on my own computer and it worked for me. Could be an outdated version of chrome or a weird os/browser combination. – Jens Væver Hartfelt Aug 14 '19 at 10:00
  • @JensVæverHartfelt all the other pc are identical to mine, same brand, same specs and operating system. the chrome version is Version 76.0.3809.100 (Official Build) (64-bit) – Emotional_Goose Aug 14 '19 at 10:21
  • Odd! Is it in a corporate environment? Perhaps some security software is preventing chrome from detecting network abailability. – Jens Væver Hartfelt Aug 14 '19 at 15:14
  • @JensVæverHartfelt indeed, ill see if there is a work around for this. – Emotional_Goose Aug 14 '19 at 15:29
  • 1
    As others have said, polling might be an ok option. If you set up an endpoint that just sends a 200 OK, it should be okay to just check that endpoint continually every 5 seconds or so. – Jens Væver Hartfelt Aug 14 '19 at 16:50
  • 1
    @JensVæverHartfelt ill give that solution a go.. thank you all for the feedback. much appreciated – Emotional_Goose Aug 15 '19 at 08:30

0 Answers0