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

时间秒杀一切 提交于 2019-12-09 17:46:25

问题


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");

回答1:


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




回答2:


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');
}


来源:https://stackoverflow.com/questions/2779367/navigator-online-still-true-when-turning-off-wifi-false-when-set-work-offline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!