EventTarget事件:unload
2019-01-27 15:52 更新
EventTarget事件 - 卸载
卸载文档或子资源时会触发该卸载事件。
它在以下情况下被触发:
- beforeunload(可取消的事件)
- pagehide
该文件处于特定状态:
- 所有资源仍然存在(IMG,IFRAME等)
- 最终用户再也看不到任何东西了
- UI交互是无效的(window.open,警报,确认等)
- 错误不会停止卸载工作流程
请注意,卸载事件也在文档树之后:父框架卸载将在子框架卸载之前发生(参见下面的示例)。
| 是否冒泡 | 没有 |
| 是否可取消 | 没有 |
| 目标对象 | DefaultView,Document,Element |
| 接口 | 如果从用户界面生成则为UIEvent,否则为Event |
| 默认操作 | 没有 |
属性
| 属性 | 类型 | 描述 |
|---|---|---|
target(只读) | EventTarget | 事件目标(DOM树中最顶层的目标)。 |
type(只读) | DOMString | 事件的类型。 |
bubbles(只读) | boolean | 事件是否正常冒泡。 |
cancelable(只读) | boolean | 该事件是否可取消。 |
view(只读) | WindowProxy | document.defaultView(window文件) |
detail(只读) | long(float) | 0。 |
示例
<!DOCTYPE html>
<html>
<head>
<title>Parent Frame</title>
<script>
window.addEventListener('beforeunload', function(event) {
console.log('I am the 1st one.');
});
window.addEventListener('unload', function(event) {
console.log('I am the 3rd one.');
});
</script>
</head>
<body>
<iframe src="child-frame.html"></iframe>
</body>
</html>下面,内容儿童frame.html:
<!DOCTYPE html>
<html>
<head>
<title>Child Frame</title>
<script>
window.addEventListener('beforeunload', function(event) {
console.log('I am the 2nd one.');
});
window.addEventListener('unload', function(event) {
console.log('I am the 4th and last one…');
});
</script>
</head>
<body>
☻
</body>
</html>卸载父框架时,将按执行console.log消息描述的顺序触发事件。
规范
| 规范 | 状态 | 注释 |
|---|---|---|
| UI事件 该规范中“卸载”的定义。 | 工作草案 |
以上内容是否对您有帮助:

免费 AI IDE


更多建议: