NavigatorOnLine.onLine

返回浏览器的联网状态。正常联网(在线)返回 true,不正常联网(离线)返回 false。一旦浏览器的联网状态发生改变,该属性值也会随之变化。当用户点击链接或者脚本进行网络请求时,如果发现浏览器连接不上互联网,则该属性会被赋值为false

各浏览器对该属性的实现有些不同。

在 Chrome 和 Safari 中,如果浏览器连接不上局域网(LAN)或者路由器,就是离线状态;否则就是在线状态。所以当该属性值为 false 的时候,你可以说浏览器不能正常联网,但如果该属性值为true的时候,并不意味着浏览器一定能连接上互联网。还有其他一些可能引起误判的原因,比如你的电脑安装了虚拟化软件,可能会有一个虚拟网卡,这时它总是会显示正常联网。因此,如果你想得到浏览器确切的联网状态,应该使用其他额外的检查手段。

在 Firefox 和 Internet Explorer 中,如果浏览器处于"脱机工作"状态,则返回 false。在 Firefox 41之前,所有其他条件都返回 true 值;在 Windows 上的 Nightly 68上测试实际行为表明,它仅查找类似 Chrome 和 Safari 的 LAN 连接,从而产生误报。

你可以在 window.ononline 和 window.onoffline上监听事件,来获取浏览器联网状态的改变情况。

语法

online = window.navigator.onLine;

取值

online 是个布尔值 truefalse。

示例

查看 在线演示.

想要查看你是否连接上了互联网,查询 window.navigator.onLine 的值,如下方示例:

if (navigator.onLine) {
  alert('online')
} else {
  alert('offline');
}

如果浏览器不支持 navigator.onLine,则上面的示例将始终显示为 false / undefined

要查看网络状态的变化,请使用 addEventListener  侦听 window.onlinewindow.offline 事件,如以下示例所示:

window.addEventListener("offline", function(e) {alert("offline");})

window.addEventListener("online", function(e) {alert("online");})

规范

Specification Status Comment
HTML Living Standard
navigator.onLine
Living Standard Initial definition

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
onLineChrome Full support 1
Notes
Full support 1
Notes
Notes Earlier versions of Chrome incorrectly return true when a tab is first opened, but it starts reporting the correct connectivity status after the first network event. Windows: 11, Mac: 14, Chrome OS: 13, Linux: Always returns true. For history, see crbug.com/7469.
Edge Full support 12Firefox Full support 3.5
Notes
Full support 3.5
Notes
Notes Since Firefox 4 the browser returns true when 'Work Offline' mode is disabled and false when it is enabled, regardless of actual connectivity. Since Firefox 41, on OS X and Windows, the returned values follow the actual network connectivity, unless 'Work offline' mode is selected (where it will always return false).
IE Full support 8
Notes
Full support 8
Notes
Notes in Internet Explorer 8 'online' and 'offline' events are raised on the document.body; under IE 9 they are raised on both document.body and window.
Opera Full support 3
Notes
Full support 3
Notes
Notes From Opera 11.1 until Opera 12.1, the browser returns true when 'Work Offline' mode is disabled and false when it is enabled, regardless of actual connectivity.
Safari Full support 5WebView Android Full support 1
Notes
Full support 1
Notes
Notes Faulty in a WebView component, see Issue bug 16760.
Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1
Notes
Full support 10.1
Notes
Notes From Opera 11.1 until Opera 12.1, the browser returns true when 'Work Offline' mode is disabled and false when it is enabled, regardless of actual connectivity.
Safari iOS Full support 4.2Samsung Internet Android Full support 1.0

Legend

Full support  
Full support
See implementation notes.
See implementation notes.

备注

See Online/Offline Events‎ for a more detailed description of this property as well as new offline-related features introduced in Firefox 3.

查看更多