检测navigator.online上的更改 [英] detect change on navigator.online

查看:102
本文介绍了检测navigator.online上的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测导航器是否将您的状态更改为在线/离线?

How can I detect if the navigator changed your state to online/offline?

类似于:

var oldState = navigator.onLine; 
window.navigator.onlinechange = function(evnt,newState) { 
         alert('your changed from' + oldState + ' to' + newState + 'state');
}


推荐答案

这样的事情(不是每一件事)浏览器支持这些事件,目前只有IE 8,9和FF> 3支持这些事件):

Something like this (not every browser supports these events, currently only IE 8,9 and FF > 3 support these events):

var el = document.body;
if (el.addEventListener) {
   el.addEventListener("online", function () {
     alert("online");}, true);
   el.addEventListener("offline", function () {
     alert("offline");}, true);
}
else if (el.attachEvent) {
   el.attachEvent("ononline", function () {
     alert("online");});
   el.attachEvent("onoffline", function () {
     alert("offline");});
}
else {
   el.ononline = function () {
     alert("online");};
   el.onoffline = function () {
     alert("offline");};
}

浏览器支持各不相同,请查看: http://help.dottoro.com/ljnasgpu.php

The browser support varies, check this out: http://help.dottoro.com/ljnasgpu.php

这篇关于检测navigator.online上的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆