So, I needed support for online / offline events in browsers (recall that according to draft
html5 they occur with the appearance / disappearance of the connection, respectively). At the moment, Mozilla and
IE8 supports these events. And this is a lot.
Everything would be fine, but an online event does not guarantee 100% that the user is online. It guarantees that there is some kind of connection, but no connection to the Internet (if someone has not seen,
explaining the video on this topic). Therefore, an additional check on the availability of the outside world. The offline event ensures that there is no connection, which can be convenient in some cases, the developer does not need to take any additional actions.
I don’t think that the implementation of these events will change over time, so if we want to know for sure if there is a connection or not, we need to add the implementation ourselves.
With Mozilla and IE8 decided (all is well, but it is necessary to file a little), but what to do with other browsers, where there are no these events yet? We will define them ourselves when initiating the page and after the Ajax requests. And we will do this using jquery with trigger / bind as a plugin.
')
The main ideas of the plugin
0) Convenient interface.
$(document.body).bind('xonline', function(event) { ... });
$(document.body).bind('xoffline', function(event) { ... });
I use xonline, xoffline since online and offline are already implemented in the browser, and we, in fact, make an add-on over these events.
1) Naturally, the plugin should support online, offline events for browsers in which these events exist. When online for these browsers there is an additional check for communication with the Internet. When offline, we do not check anything.
2) For browsers that do not support these actions, checking for a change in network status occurs after ajax requests. Suppose there was a connection, and the request was unsuccessful. In this case, we do an additional check on the connection as in point 1 and, if necessary, trigger the trigger ('xoffline').
How to use?
It's simple, create handlers for two xonline, xoffline events. The rest of the plugin will perform itself. You can see how the plugin works by opening
an example and then disconnecting from the network. In the eighth, ie, and firefox 3.5 will work xoffline and the rectangle will be repainted. In google chrome, opera, safari and earlier versions of ie and mozilla, in addition to disconnecting from the network, to run the offline event you also need to perform a failed request. If it is vital to monitor the connection all the time, the plugin supports the $ .checkWebstatus (time) method (time is the number of seconds). This method in browsers that do not support native online / offline events checks the connection every time seconds.
CheckWebStatus exampleSuccessfully tested in Mozilla 3.5.3, IE8, IE6, Chrome, Opera 10
Download plugin Example Example with additional verification of the plug-in's interiorUpdate 10.10.2009Corrected the plugin:
1) Fixed bug with offline network status checking.
2) Events renamed from xonline, xoffline to more understandable trueonline, trueoffline.
3) Removed methods and variables from window.
4) Added the ability to customize the plugin.
5) The plugin has
its own page.6) Now it is called TrueOnline plugin :)
Thank you all for your wise advice and testing.