简体   繁体   中英

How to detect if browser support offline event?

I've try to use code from this question: How to detect `focusin` support?

but for chromium that spport offline event hasEvent('offline') return false. Anybody know how to detect offline/online events in JavaScript?

Try:

return !!window.applicationCache;

If I understand properly your question, you also may want to read this article .

EDIT:

Basically you can use the following function:

function reportConnectionEvent(e)
{
    if (!e) e = window.event;

    if ('online' == e.type) {
        alert( 'The browser is ONLINE.' );
    }
    else if ('offline' == e.type) {
        alert( 'The browser is OFFLINE.' );
    }
    else {
        alert( 'Unexpected event: ' + e.type );
    }
}
window.onload = function() {
    document.body.ononline = reportConnectionEvent;
    document.body.onoffline = reportConnectionEvent;
}  

Also you may check this for demo http://html5demos.com/offline-events

You can try

'onoffline' in window

or

'onoffline' in document.body

It seems that online and offline events start on body and bubble up so you can't use div to detect it. But I've created this code:

var body = document.getElementsByTagName('body')[0];
body.setAttribute('ononline', 'return;')
typeof body.ononline == 'function';

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