6

navigator.onLine is still returning true when I turn off Wi-Fi (Airport on my notebook in OS X). This is counterintuitive behavior. But when I set "work offline" in a browser like Firefox, it correctly returns false. Is this expected?

alert(navigator.onLine ? "online" : "offline");
chimerical
  • 5,603
  • 8
  • 30
  • 36
  • Firefox's (and IE's and Opera's) implementation is wrong. See my comment to that effect here: https://bugzilla.mozilla.org/show_bug.cgi?id=654579#c9 – thewoolleyman Mar 11 '12 at 09:21

2 Answers2

3

Yes. The browser doesn't provide network connectivity information to the page, but rather uses Work Offline's status as the value.

Delan Azabani
  • 76,631
  • 25
  • 162
  • 208
  • It seems odd because if I turn off Wi-Fi and cellular data by turning on airplane mode on an iPhone, and then return to that page without reloading, it returns true as offline (which is correct). – chimerical May 06 '10 at 16:10
  • Browsers may want to detect if network connectivity is down *and then* set the Work Offline variable to `false`. However, the user may always change this variable herself. – Delan Azabani May 08 '10 at 00:27
1

Use the window.addEventListener to detect network updates:

window.addEventListener('online',  amIOnline);
window.addEventListener('offline', amIOffline);

function amIOnline(){
    console.log('online');
}

function amIOffline(){
    console.log('offline');
}
nweg
  • 2,765
  • 2
  • 20
  • 30
Fritoebola
  • 11
  • 1