unload
unload
イベントは、文書または子リソースがアンロードされるときに発生します。
以下のイベントの後に発生します。
beforeunload
(キャンセル可能なイベント)pagehide
文書は以下のような状態にあります。
- すべてのリソースがまだ存在する (img、iframe など)
- エンドユーザーから見えるものは何もない
- UI でのやり取りは効果がない (
window.open
,alert
,confirm
, など) - エラーが発生しても、アンロードの処理の流れは停止しない
unload イベントは文書ツリーにも続くことに注意してください。親フレームのアンロードは、子フレームの unload
の前に行われます (以下の例を参照)。
例
html
<!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
の内容を以下に示します。
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()
のメッセージに記述された順序でイベントが発生します。
仕様書
Specification |
---|
HTML Standard # event-unload |
HTML Standard # handler-window-onunload |
ブラウザーの対応
BCD tables only load in the browser