如何在JavaScript中检测Internet连接是否离线?


当前回答

下面是我拥有的一个辅助实用程序片段。这是带名称空间的javascript:

network: function() {
    var state = navigator.onLine ? "online" : "offline";
    return state;
}

你应该使用这个方法检测,否则发射一个'替代'的方式来做这件事。这将是我们所需要的全部。其他的方法是hack。

其他回答

对于两种不同的情况,有两个答案:-

If you are using JavaScript on a website(i.e; or any front-end part) The simplest way to do it is: <h2>The Navigator Object</h2> <p>The onLine property returns true if the browser is online:</p> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = "navigator.onLine is " + navigator.onLine; </script> But if you're using js on server side(i.e; node etc.), You can determine that the connection is lost by making failed XHR requests. The standard approach is to retry the request a few times. If it doesn't go through, alert the user to check the connection, and fail gracefully.

HTML5应用程序缓存API指定导航器。onLine,目前在IE8测试版中可用,WebKit(例如。Safari)的夜间运行,并且已经在Firefox 3中得到支持

几乎所有主流浏览器现在都支持window.navigator. online属性,以及相应的在线和离线窗口事件。运行以下代码片段来测试它:

console.log('Initially ' + (window.navigator.onLine ? 'on' : 'off') + 'line'); window.addEventListener('online', () => console.log('Became online')); window.addEventListener('offline', () => console.log('Became offline')); document.getElementById('statusCheck').addEventListener('click', () => console.log('window.navigator.onLine is ' + window.navigator.onLine)); <button id="statusCheck">Click to check the <tt>window.navigator.onLine</tt> property</button><br /><br /> Check the console below for results:

尝试将您的系统或浏览器设置为离线/在线模式,并检查日志或window.navigator.onLine属性的值变化。

请注意Mozilla文档中的这段话:

In Chrome and Safari, if the browser is not able to connect to a local area network (LAN) or a router, it is offline; all other conditions return true. So while you can assume that the browser is offline when it returns a false value, you cannot assume that a true value necessarily means that the browser can access the internet. You could be getting false positives, such as in cases where the computer is running a virtualization software that has virtual ethernet adapters that are always "connected." Therefore, if you really want to determine the online status of the browser, you should develop additional means for checking. In Firefox and Internet Explorer, switching the browser to offline mode sends a false value. Until Firefox 41, all other conditions return a true value; since Firefox 41, on OS X and Windows, the value will follow the actual network connectivity.

(重点是我自己的)

这意味着如果window.navigator.onLine为false(或者您得到一个脱机事件),则保证您没有Internet连接。

然而,如果它是真的(或者你得到一个在线事件),它充其量只意味着系统连接到某个网络。这并不意味着你可以上网。要检查这一点,您仍然需要使用其他答案中描述的解决方案之一。

我最初打算把这篇文章作为格兰特·瓦格纳(Grant Wagner)的回答的更新,但这似乎太过编辑了,尤其是考虑到2014年的更新已经不是他的了。

有很多方法可以做到这一点:

AJAX请求到您自己的网站。如果请求失败,很有可能是连接有问题。JQuery文档中有关于处理失败AJAX请求的章节。这样做时要注意同源策略,这可能会阻止您访问域外的网站。 你可以在img中添加一个onerror,比如<img src="http://www.example.com/singlepixel.gif" onerror="alert('Connection dead');"/ >。

如果源图像被移动/重命名,此方法也可能失败,并且通常是一个不如ajax选项的选择。

因此,有几种不同的方法来尝试和检测这种情况,没有一种是完美的,但在没有跳出浏览器沙箱并直接访问用户的网络连接状态的能力的情况下,它们似乎是最好的选择。

IE 8将支持window.navigator.onLine属性。

但当然,这对其他浏览器或操作系统没有帮助。考虑到了解Ajax应用程序中的在线/离线状态的重要性,我预计其他浏览器供应商也将决定提供该属性。

在此之前,XHR或Image()或<img>请求都可以提供接近您想要的功能。

更新(2014/11/16)

大多数浏览器现在都支持这个属性,但结果会有所不同。

引用自Mozilla文档:

In Chrome and Safari, if the browser is not able to connect to a local area network (LAN) or a router, it is offline; all other conditions return true. So while you can assume that the browser is offline when it returns a false value, you cannot assume that a true value necessarily means that the browser can access the internet. You could be getting false positives, such as in cases where the computer is running a virtualization software that has virtual ethernet adapters that are always "connected." Therefore, if you really want to determine the online status of the browser, you should develop additional means for checking. In Firefox and Internet Explorer, switching the browser to offline mode sends a false value. All other conditions return a true value.