2 ways for JavaScript to determine whether a machine is networked _javascript tips

Source: Internet
Author: User
In many scenarios, Web applications can be started only after the machines have been networked. If there is no networking, you will be prompted for an error. 、
But the machines sometimes have to be restarted. If the machine reboots and starts the Web application immediately, it may be that the network service on the machine is not ready.

Especially Windows7, it takes several seconds to start a network service.
Several methods have been tried before:
For example, ping to determine, but the ping needs to have a non-native IP address. This is not very versatile.
For example, monitoring whether a port has been occupied, but the optical port occupancy, also does not indicate that the network has been started.
Another aspect, if it's a Web application, it's best to judge on the front end, like using JavaScript.
There are also people who write JavaScript ping simulations on the web. But it does have some trouble.
What to do, when the key, or HTML5 can give a good solution:

method One:
Navigator.online
Copy Code code as follows:

if (navigator.online)
{//normal work}
else {//tasks when offline state is performed}

The new features of this HTML5 navigator can be very simple for us to handle.
HTML5 defines a Navigator.online property that is true to indicate that the device is able to surf the internet, and a value of false indicates that the device is offline.
Of course, different browsers, support for this is not quite the same
ie6+ and Safari 5+ are better supported.
Firefox 3+ and Support Navigator.online properties, but you must manually select the menu item "File-web Developer (Setup)-Work offline" to get the browser working properly.
Chrome needs more than 12.

Method Two:
Of course, if you want to support more compatible, you can use the following 2 events: Online and offline. These two events are triggered 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 the navigator.online. Then, it is through these two events to determine whether the network connection state changes. When the above event triggers, the value of the Navigator.online property also changes, but you must manually poll this property to detect changes to the network state.
Copy Code code as follows:

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");
});
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.