0

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

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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
jcubic
  • 61,973
  • 54
  • 229
  • 402

3 Answers3

0

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

Bud Damyanov
  • 30,171
  • 6
  • 44
  • 52
  • I don't need to have app that work offline, I need to detect the event because I need to add ajax request to queue and when internet is back I need to send them. – jcubic Aug 13 '14 at 11:24
0

You can try

'onoffline' in window

or

'onoffline' in document.body

Yury Tarabanko
  • 44,270
  • 9
  • 84
  • 98
  • this not bulletproff check, every even check test also check if event work correctly not only if it's present. – jcubic Aug 21 '14 at 13:54
  • @jcubic LOL how would you check it "works correctly"? :) – Yury Tarabanko Aug 21 '14 at 14:58
  • And this method is widely used and robust enough for modernizr but somehow not for you (https://github.com/Modernizr/Modernizr/blob/master/src/isEventSupported.js). Kinda not cool to accept your own answers making stuff in more complex manner. – Yury Tarabanko Aug 21 '14 at 15:05
  • I've seen this in other SO question about events http://stackoverflow.com/questions/2877393/detecting-support-for-a-given-javascript-event where you test if the value is a function. – jcubic Aug 22 '14 at 11:48
0

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';
jcubic
  • 61,973
  • 54
  • 229
  • 402