Js-Determine if the user is connected to the Internet (Internet connection)-HTML5 online, offline use

Source: Internet
Author: User

(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

Related Article

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.