Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are online and offline events not firing in Firefox?

I am attempting to implement a JS function to get invoked whenever there is a connectivity change, another-words, when the internet connection is lost or regained.

I found an example of this on Mozilla forums: https://developer.mozilla.org/en-US/docs/Online_and_offline_events

Here is my code:

    $(document).ready(function () {

        window.addEventListener('offline', function () {
            alert('Internet connection lost');
        });

        window.addEventListener('online', function () {
            alert('Internet connection restored');
        });
    });

However miraculously this works in IE and Chrome, but not in Firefox. (The test on the Mozilla forum behaves the same way)

I tried adding onoffline/ononline parameters to the body tag, with no luck either.

Is there something I am missing here?

like image 897
Razor Avatar asked Jan 21 '26 08:01

Razor


1 Answers

Firefox has a different interpretation of the navigator.onLine attribute.

As you can read here:

We return false when we're in offline mode. Firefox 4 only enters offline mode if the user explicitly requests so (it's an option in the file menu). If we're not in offline mode, we will try to access the network if you try to "follow a link" or "request a remote page". So per spec, we're returning true unless we're in offline mode.

For a lot of users this is not the expected behavior. So it seems that they have already changed it in the 41.0a2 release.

Firefox 41 will ship in September 2015.

So I am afraid that you will need to wait a little to see it works ;)

Anyway, we can not rely too much on this attribute.

Hope it helps!

like image 114
David Avatar answered Jan 23 '26 21:01

David



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!