Remy Sharp blogged recently about the issues of the
navigator.onLine property and online/offline events. Let see how they can be worked around.
Getting the browser to handle it is hard
As it turns out, this property and those events can currently only be relied on to tell whether and when the user decides to turn on/off the “Work offline” feature of the browser. Indeed, some platforms have specific behaviors that make it hard for the browser to be constantly aware of the computer connectivity: Windows sometime incorrectly report its connectivity after waking up from hibernation, NetworkManager on Linux can’t be trusted because some users bypass it, etc.
This is why, as of Firefox4 and until we can find a reliable alternative, only the user can switch to offline mode.
onLine doesn’t tell
If you are building a website with offline features, using this property to switch between online and offline mode might not be the best option. Not only can it incorrectly report the connectivity of the computer, but it also doesn’t tell anything about the actual reachability of the server: you might be connected to a network blacklisting the service; you might have no connection but be developing a website locally; or the server could be down…
In any case, it is probably more reliable to simply send a request to the server and fallback to local storage/resources/database when the request fails.
Following is an example of code you can use to verify that the server is reachable