Ar eventutil = {addhandler:function (element, type, handler) {if (Element.addeventlistener) {Element.addeventlistener ( Type, handler, false); } else if (element.attachevent) {element.attachevent ("on" + type, handler), or else {element["on" + type] = handler;}} };
Eventutil.addhandler (window, "online", function () {alert ("online");
}); Eventutil.addhandler (window, "Offline", function () {
Alert ("Offline"); }); Of course, if you want to support more compatible, you can use the following 2 events: Online and offline. These two events are triggered separately when the network changes from offline to online or from online to offline. These two events are triggered on the Window object.
In order to detect whether the application is offline, after the page is loaded, it is best to get the initial state through Navigator.online. Then, it is through the two events above to determine whether the network connection status changes. When the above event is triggered, the value of the Navigator.online property will change, but it must be polled manually to detect changes in network state. Method One: The Navigator.online copy Code code is as follows: if (navigator.online) {//normal work} else {//Perform offline state task} This HTML5 navigator new feature can be very simple to help us.
HTML5 defines a Navigator.online property for this property, which is true to indicate that the device can surf the internet and a value of false indicates that the device is offline.
Of course different browsers, the support for this is not quite the same ie6+ and Safari 5+ support the relatively good Firefox and support navigator.online properties, but you must manually select the menu item "File-web Developer (Setup)-Work Offline" To allow the browser to work properly.
Chrome requires more than 12.
JS to determine if there is no network (mobile side)