Home  >  Article  >  Web Front-end  >  2 ways to determine whether a machine is connected to the Internet using javascript_javascript skills

2 ways to determine whether a machine is connected to the Internet using javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:26:06946browse

In many scenarios, web applications can only be started after the machine is connected to the Internet. If there is no Internet connection, an error will be prompted. ,
But the machine sometimes needs to be restarted. If the web application is started immediately after the machine is restarted, it is possible that the network service on the machine is not ready yet.

Especially in Windows 7, it takes several seconds to start the network service. What should I do at this time?
I have tried several methods before:
For example, judging by ping, but ping requires a non-local IP address. This is not very general
For example, monitoring whether a certain port is occupied, but if the optical port is occupied, it does not mean that the network has been started.
On the other hand, if it is a web application, it is best to judge on the front end, such as using javascript.
There are also people on the Internet who write javascript to simulate the effect of ping. But it does have some trouble.
What to do? When it comes to critical situations, HTML5 can give you a good solution:

Method 1:
navigator.onLine

Copy code The code is as follows:

if (navigator.onLine)
{ //Works normally}
else { //Perform tasks in offline state}

This new feature of HTML5 navigator can easily help us do it
HTML5 defines a navigator.onLine attribute for this, and the value of this attribute is true Indicates that the device can access the Internet. A value of false indicates that the device is offline.
Of course, different browsers have different support for this
IE6 and Safari 5 support it better
Firefox 3 supports the navigator.onLine attribute, but you must manually select the menu item "File- Web Developer (Settings) - Work Offline" to make the browser work properly.
Chrome requires 12 or above.

Method 2:
Of course, if you want to support more compatibility, you can use the following two events: online and offline. These two events are triggered respectively 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 obtain the initial status through navigator.onLine. Then, the above two events are used to determine whether the network connection status changes. When the above event is triggered, the value of the navigator.onLine property will also change, but this property must be manually polled to detect changes in network status.
Copy code The code is 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");
});
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn