如何检测跨浏览器的在线/离线事件? [英] How to detect online/offline event cross-browser?

查看:188
本文介绍了如何检测跨浏览器的在线/离线事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用HTML5在线和离线活动准确检测浏览器何时脱机。

I'm trying to accurately detect when the browser goes offline, using the HTML5 online and offline events.

这是我的代码:

<script>
    // FIREFOX
    $(window).bind("online", applicationBackOnline); 
    $(window).bind("offline", applicationOffline);

    //IE
    window.onload = function() {
        document.body.ononline = IeConnectionEvent;
        document.body.onoffline = IeConnectionEvent;
    } 
</script>

当我在Firefox或IE上点击脱机工作时它工作正常,但它有点像当我实际上拔下电线时随机工作。

It works fine when I just hit "Work offline" on either Firefox or IE, but it's kind of randomly working when I actually unplug the wire.

检测此更改的最佳方法是什么?我想避免使用超时重复ajax调用。

What's the best way to detect this change? I'd like to avoid repeating ajax calls with timeouts.

推荐答案

浏览器供应商无法就如何离线定义达成一致。某些浏览器具有脱机工作功能,他们认为这与缺乏网络访问是分开的,这与互联网访问不同。整件事情一团糟。一些浏览器供应商在实际网络访问丢失时更新navigator.onLine标志,其他浏览器供应商则不会。

The browser vendors cannot agree on how to define offline. Some browsers have a Work Offline feature, which they consider separate to a lack of network access, which again is different to internet access. The whole thing is a mess. Some browser vendors update the navigator.onLine flag when actual network access is lost, others don't.

来自规范:


如果用户代理是
肯定是离线的,则返回false(与
网络断开连接)。如果用户
代理可能在线,则返回true。

Returns false if the user agent is definitely offline (disconnected from the network). Returns true if the user agent might be online.

当此属性的值为
时,联机和离线事件为
更改。

The events online and offline are fired when the value of this attribute changes.

navigator.onLine属性必须
如果用户代理将
在用户
跟随时未联系网络,则返回false链接或当脚本
请求远程页面时(或者知道
这样的尝试会失败),并且
必须返回true。

The navigator.onLine attribute must return false if the user agent will not contact the network when the user follows links or when a script requests a remote page (or knows that such an attempt would fail), and must return true otherwise.

最后,规格说明:


此属性本身就是
不可靠。计算机可以是
连接到网络而无需b $ b上网。

This attribute is inherently unreliable. A computer can be connected to a network without having Internet access.

这篇关于如何检测跨浏览器的在线/离线事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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