From 235c34993c7b14f783fc8259cc237ac09f0d3e57 Mon Sep 17 00:00:00 2001 From: MDN Date: Fri, 9 Jul 2021 00:38:08 +0000 Subject: [CRON] sync translated content --- .../web/api/navigatoronline/online/index.html | 88 ++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 files/zh-cn/orphaned/web/api/navigatoronline/online/index.html (limited to 'files/zh-cn/orphaned/web/api/navigatoronline/online/index.html') diff --git a/files/zh-cn/orphaned/web/api/navigatoronline/online/index.html b/files/zh-cn/orphaned/web/api/navigatoronline/online/index.html new file mode 100644 index 0000000000..ffc33f9d7a --- /dev/null +++ b/files/zh-cn/orphaned/web/api/navigatoronline/online/index.html @@ -0,0 +1,88 @@ +--- +title: NavigatorOnLine.onLine +slug: orphaned/Web/API/NavigatorOnLine/onLine +tags: + - API + - DOM Reference +translation_of: Web/API/NavigatorOnLine/onLine +original_slug: Web/API/NavigatorOnLine/onLine +--- +

{{ApiRef("HTML DOM")}}

+ +

返回浏览器的联网状态。正常联网(在线)返回 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");})
+
+ +

规范

+ + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName("HTML WHATWG", "browsers.html#dom-navigator-online", "navigator.onLine")}}{{Spec2("HTML WHATWG")}}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.

+ +

{{Compat("api.NavigatorOnLine.onLine")}}

+ +

备注

+ +

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

+ +

查看更多

+ + -- cgit v1.2.3-54-g00ecf