如何检测导航器是否将您的状态更改为在线/离线?
就像是:
var oldState = navigator.onLine;
window.navigator.onlinechange = function(evnt,newState) {
alert('your changed from' + oldState + ' to' + newState + 'state');
}
解决方法:
这样的事情(不是每个浏览器都支持这些事件,目前只有IE 8,9和FF> 3支持这些事件):
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