此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

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 2015年7月.

当浏览器能够访问网络,且 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

浏览器兼容性

参见