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");
onLine. Returns the online status of the browser. The property returns a boolean value, with true meaning online and false meaning offline.
Yes. The browser doesn't provide network connectivity information to the page, but rather uses Work Offline's status as the value.
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');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With