3

JavaScript navigator.onLine command suddenly stop working on Android phones, not in Android Browser, also not in Chrome!

In Android Galaxy, as soon as you put on flight mode (or there is no internet), you get a notification that there is no internet in the following code - but you immediately get a message as if the internet is back.

The following code for example stopped working:

        window.addEventListener('offline', function (e) {
            alert('offline');
        });
        window.addEventListener('online', function (e) {
            alert('online');
        });

And

navigator.onLine

always returns true!

Does anyone have any idea, why is this happening?

Check it yourself on Android: https://codepen.io/Zvi-Redler/pen/JjeMLWR

Or an idea how to get around the problem in the meantime?

(Our QA team found, checked on several devices, we don't know about a special update. I do airplane mode, for sure there is no network and in the past it worked.)

Zvi Redler
  • 1,708
  • 1
  • 18
  • 29
  • 1
    `following code for example stopped working` - so, it did work and now it doesn't? What has changed? Chrome update? how are you testing? – Jaromanda X Jul 12 '23 at 08:33
  • According to https://developer.mozilla.org/en-US/docs/Web/API/Window/offline_event#browser_compatibility, this should have "full support" in all browsers listed there. _"Does anyone have any idea why?"_ - maybe you are not actually as offline as you think you are? The event _"is fired when the browser has lost access to the network"_, according to MDN. – CBroe Jul 12 '23 at 08:39
  • It worked and stopped working. Our QA team found, checked on several devices, we don't know about a special update. I do airplane mode, for sure there is no network and in the past it worked. – Zvi Redler Jul 12 '23 at 08:48
  • Are you on a VPN? – mplungjan Jul 12 '23 at 09:04
  • No, and the Wi-Fi network and the network of the cellular company give the same result, In the past we used to put 'flight mode' on and get the indication - today the event not fired – Zvi Redler Jul 12 '23 at 09:18
  • A long shot here... but consider removing/disabling any antivirus you may have; I had issues with network status being reported wrongly before – rjanjic Jul 23 '23 at 21:40

3 Answers3

2

First window's offline event, it fires when there is a network change. Second, navigator.onLine, it returns the current status of the network. Both are different.

On Samsung Galaxy J7 (2015), Chrome 104 it is working properly. After I load the page and turn off the data, an alert with the current network state is appearing, so window offline event working properly. And on clicking the button, navigator.onLine is return true, if I'm online, else returning false.

Everything seems to work fine.

  • In my galaxy FE8 it is not working. Put your cell phone on airplane mode. I receive an OFFLINE message and immediately an ONLINE message! Although there is nothing to do with the network or the Internet. https://codepen.io/Zvi-Redler/pen/JjeMLWR – Zvi Redler Jul 25 '23 at 05:17
  • In my case, Everything works fine! I've tried the same, it didn't gave me 'online' message. You should try with some other phone or with different browsers. – Codemaster United Jul 25 '23 at 07:42
1

You can try using a combination of other events to detect the online/offline status like the online and offline events on the document object, along with a periodic check using setInterval to monitor the network status.

function checkOnlineStatus() {
  if (navigator.onLine) {
    alert('Online');
  } else {
    alert('Offline');
  }
}

checkOnlineStatus();

// Check online status as needed
setInterval(checkOnlineStatus, 2000);

// Listen for the 'online' and 'offline' events
document.addEventListener('online', function() {
  alert('Online');
});

document.addEventListener('offline', function() {
  alert('Offline');
});
Wakil Ahmed
  • 1,053
  • 1
  • 8
  • 16
  • It is not working. I get "online" even when it is offline! Put your cell phone on airplane mode. I receive an OFFLINE message and immediately an ONLINE message! Although there is nothing to do with the network or the Internet. https://codepen.io/Zvi-Redler/pen/JjeMLWR – Zvi Redler Jul 25 '23 at 05:18
1
window.addEventListener('offline', function (e) {
  alert('offline');
});

IS NOT EQUAL TO navigator.onLine

just keep your function and remove the navigation.onLine

s1ddiq
  • 11
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 26 '23 at 16:12