Window:offline 事件
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.
offline
事件在浏览器失去网络连接时,在 Window
接口上触发。并且 Navigator.onLine
的值变为 false
。
语法
在类似 addEventListener()
这样的方法中使用事件名称,或者设置事件处理器属性。
js
addEventListener("offline", (event) => {});
onoffline = (event) => {};
事件类型
一个 Event
。
事件处理方法别名
除了 Window
接口以外,事情处理方法属性 onoffline
同样可以用于以下目标:
示例
js
// addEventListener 版本
window.addEventListener("offline", (event) => {
console.log("网络连接已断开。");
});
// onoffline 版本
window.onoffline = (event) => {
console.log("网络连接已断开。");
};
规范
Specification |
---|
HTML # event-offline |
HTML # handler-window-onoffline |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
offline event |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
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.