简体   繁体   中英

How can I use onoffline and ononline events in Chrome and other modern browsers?

I see that ononline and onoffline events are only available on Firefox and IE , but not for the other browsers (Chrome, Opera, Safari). How could I set the events in Chrome and other modern browsers?

Use navigator.onLine for all browser support as it is mentioned in link you provided. To whomever said that question is about checking if user is online - that's php's or what ever you prefer to use part not JS's.

According to Bas van Dijk, I make it closer to perfection.

function setOnOffline(onOnline=function (){},onOffline=function (){}){
    window.online=onOnline;
    window.offline=onOffline;
    window.last123=navigator.onLine;registered
    setInterval(function (){
    if (!(window.last123==navigator.onLine)){
        if (navigator.onLine){
            window.online();
        }else{
            window.offline();
        }
        window.last123=navigator.onLine;
    }
    },1000/60);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM