These two years of offline browsing technology is becoming more and more popular, the most common is the HTML5 mobile application, there are many common web apps also use these technologies. However, the advent of new technologies can sometimes lead to additional distress for our web developers, such as how to tell if a user is now online or offline? Fortunately, there are spears and shields, and the Navigator object in JavaScript helps us solve this problem.
The Navigator.online property provides us with a Boolean value to determine whether the user is connected to the Internet. You can access it like this:
if (navigator.online) {//True|false
// ...
}
No, it's easier than that!
Event
In addition to the ability to detect this offline/online attribute value, we can also bind the offline and AOL events:
function Updateindicator () {
This can be based on the color of the Offline/online button
}
Update online status based on network connectivity
Window.addeventlistener (' online ', updateindicator);
Window.addeventlistener (' Offline ', updateindicator);
Updateindicator ();
Jquery+settimeout can also be used to detect the time between offline/online
var getonline = function () {
if (navigator.online) {
Online time processing
}else{
Updateindicator ();
}
Window.settimeout (Getonline, 1000); Core statements are detected at intervals of 1 seconds
};
jquery calls
$ (document). Ready (function () {getonline ();});
Detect offline/Online status with Javascript/jquery