(The test for compatibility is not complete yet)
Judging by the online properties of the new Navigator feature of HTML5, it is easy to decide whether or not to judge on line (true: online; false: Offline).
Compatibility:
(tested) ie6+, Safari 5+ support is better;
Firefox also supports the Navigator.online attribute, but you must manually check the menu item "File-web Developer (Setup)-Work offline" to make the browser work properly;
Chrome needs more than 12 (not tested);
1, the Code implementation:
if (window.navigator.online==true) {
Alert ("first-connected");
}else {
Alert ("First-not Connected");
}
Window.addeventlistener ("Online", online, false);
Window.addeventlistener ("Offline", offline, false);
function Online () {alert ("Reconnect"); }
function offline () {alert ("disconnection"); }
2, more compatible with the wording:
var eventutil = {
Addhandler:function (element, type, handler) {
if (Element.addeventlistener) {
Element.addeventlistener (type, handler, false);
}else if (element.attachevent) {
Element.attachevent ("On" + type, handler);
}else {
Element["on" + type] = handler;
}
}
};
Eventutil.addhandler (window, "online", function () {alert ("Online");});
Eventutil.addhandler (window, "Offline", function () {alert ("Offline");});
3. Analysis and steps:
To detect if an app is offline, listen for events: Online and offline. When the network changes from offline to online or from online to offline, the events (triggered on the Window object) are triggered separately.
First: After the page is loaded, it is best to get the initial state through Navigator.online.
Second: The monitoring of the above two events to determine whether the network connection status changes. (Navigator.online property value)
The level is limited, the error in the text is unavoidable, welcome to criticize the suggestion comment. The article will revise and perfect the treatise on an irregular basis. Thank you!
This article refers to: HTML5 offline power usage can be explained
Http://www.educity.cn/wenda/11745.html
Js-Determine if the user is connected to the Internet (Internet connection)-HTML5 online, offline use