Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

navigator.onLine still true when turning off WiFi, false when set "work offline" in browser

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");
like image 960
chimerical Avatar asked May 06 '10 07:05

chimerical


People also ask

Can I use Navigator online?

onLine. Returns the online status of the browser. The property returns a boolean value, with true meaning online and false meaning offline.


2 Answers

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

like image 164
Delan Azabani Avatar answered Oct 12 '22 06:10

Delan Azabani


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');
}
like image 42
Fritoebola Avatar answered Oct 12 '22 07:10

Fritoebola