--- title: unload slug: Web/API/Window/unload_event tags: - Window - events - unload translation_of: Web/API/Window/unload_event original_slug: Web/Events/unload ---
{{APIRef}}
当文档或一个子资源正在被卸载时, 触发 unload事件。
可冒泡(Bubbles) | No |
---|---|
可取消(Cancelable) | No |
接口(Interface) | {{domxref("Event")}} |
事件处理程序属性(Event handler property) | {{domxref("WindowEventHandlers/onunload", "onunload")}} |
它在下面两个事件后被触发:
文档处于以下状态:
window.open
, alert
, confirm
等.)请注意unload
事件也遵循文档树:父iframe会在子iframe卸载前卸载(参考下面的例子).
<!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>
下面是 child-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>
当父iframe被卸载,事件将按console.log()
消息描述的顺序触发。
规范 | 状态 | 描述 |
---|---|---|
{{SpecName('UI Events', '#event-type-unload', 'unload')}} | {{Spec2('UI Events')}} |
{{Compat("api.Window.unload_event")}}