JS to determine if there is no network (mobile side)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.