Window:online 事件

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

当浏览器能够访问网络,且 Navigator.onLine 的值被设为 true 时,Window 接口的 online 事件将被触发。

备注: 该事件不能用于确定某个网站可否访问。网站自身问题或防火墙都有可能阻止对特定网站的访问。

是否冒泡
是否可取消
接口 Event
Event handler property ononline

示例

js
// addEventListener version
window.addEventListener("online", (event) => {
  console.log("You are now connected to the network.");
});

// ononline version
window.ononline = (event) => {
  console.log("You are now connected to the network.");
};

规范

Specification
HTML
# event-online
HTML
# handler-window-ononline

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
online event

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

参见