* 仕様書

[REFS[
- [22] [CITE@en-GB-x-hixie[HTML Standard]] ([TIME[2015-04-25 04:40:19 +09:00]] 版) <https://html.spec.whatwg.org/#unloading-documents>
- [17] [CITE@en[Page Visibility]] ([TIME[2013-05-20 14:33:13 +09:00]] 版) <https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/PageVisibility/Overview.html#pv-unloaded>
]REFS]

* 文書の unload

[24] [DFN[[RUBYB[文書を unload]@en[unload a document]]]] [SRC[>>22]] するには、

[FIG(list members)[

: [VAR[文書]] : [[unload]] する[[文書]]。
: [VAR[再帰フラグ]] : [[boolean]]。[[再帰呼び出し]]かどうかを表します。
既定値は[[偽]]です。

]FIG]

... について、次のようにします [SRC[>>22]]。

[FIG(steps)[
= [[文書]]の [[unload a document]] 呼び出し済みフラグを[[真]]に設定します。
([CODE(JS)@en[[[document.open]]]] で使います。)
= [25] [VAR[文書]]の[F[イベントループ]]の [F[termination nesting level]] を、
[N[1]] [[インクリメント]]します。
= [46] [VAR[文書]]の [F[ignore-opens-during-unload counter]] を、
[N[1]] [[インクリメント]]します。
= [47] [F[文書]]の [F[page showing]] が[[真]]の場合、
== [48] [VAR[文書]]の [F[page showing]] を、[[偽]]に設定します。
== [49] [[イベントを発火]]します。
[FIG(list members middle)[

:[[インターフェイス]]:[CODE(DOMi)@en[[[PageTransitionEvent]]]]
:[[イベント型]]:[CODE(DOMe)@en[[[pagehide]]]]
:[[対象]]:[[文書]]の [CODE(DOMi)@en[[[Window]]]]
:[[target override]]:[[文書]]
:[[trusted]]:[[真]]
:[[bubbles]]:[[偽]]
:[[取り消し可能]]:[[偽]]
:[CODE(DOMa)@en[[[persisted]]]]:[[文書]]の [[''salvageable'']]

]FIG]
== [50] [VAR[文書]]について[[unloading document visibility change steps]] を実行します。
= [36] [VAR[文書]]の [F[fired unload]] が[[偽]]の場合、
== [35] [[イベントを発火]]します。
[FIG(list members middle)[

:[[イベント型]]:[CODE(DOMe)@en[[[unload]]]]
:[[対象]]:[[文書]]の [CODE(DOMi)@en[[[Window]]]]
:[[target override]]:[[文書]]

]FIG]
= [38] [VAR[文書]]の[F[イベントループ]]の [F[termination nesting level]] を、
[N[1]] [[デクリメント]]します。
= [39] 先の [CODE(DOMe)@en[[[unload]]]] [[イベント]]の[[発火]]で何らかの[[イベントリスナー]]が実行されていれば、
== [52] [VAR[文書]]の [F[''salvageable'']] を、[[偽]]に設定します。
== [53] [VAR[文書]]の [F[fired unload]] を、[[真]]に設定します。
= [51] [VAR[文書]]について [[unloading document cleanup steps]] を実行します。
= [37] [VAR[再帰フラグ]]が[[偽]]の場合、
== [54] [VAR[文書]]の[F[子孫閲覧文脈のリスト]]の各[[閲覧文脈]][VAR[閲覧文脈]]について、
=== [55] [VAR[閲覧文脈]]の[F[活性文書]]について [[unload a document]] を実行します。
[VAR[再帰フラグ]]は[[真]]とします。
=== [56] [VAR[閲覧文脈]]の[F[活性文書]]の [F[''salvageable'']] が[[偽]]の場合、
==== [57] [VAR[文書]]の [F[''salvageable'']] を、[[偽]]に設定します。
== [58] [VAR[文書]]の [F[''salvageable'']] が[[偽]]の場合、
=== [59] [VAR[文書]]を[[捨てます][文書を捨てる]]。
= [60] [VAR[文書]]の [F[ignore-opens-during-unload counter]] を、
[N[1]] [[デクリメント]]します。
]FIG]

[23] [[文書]]は、 [DFN[[F[fired unload]]]] フラグを持ちます。
初期値は[[偽]]です [SRC[>>22]]。
[[文書のunload]]で参照される他に、 [CODE(JS)@en[document.open]]
でも値が設定されます。

[26] [[unload a document]] の実行中か否かは、[[文書読み込みを停止]]の処理に影響します。

[31] ⊿ を指定した[[履歴の探索]]は、 [[unload a document]] を呼び出すことがあります。

[27] [[unload a document]] の実行中は、⊿ を指定した[[履歴の探索]]は行われません。

[28] [[unload a document]] の実行中は、 [[navigate]] は行われません。

[29] [[navigate]] は [[unload a document]] を呼び出すことがあります。
その呼び出し中に [[navigate]] が取り消されたとしても、 [[unload a document]]
の実行は中断されません。

;; [[navigate]] を参照。

[33] [[閲覧文脈を閉じる]]と、[[unload a document]] が呼び出されることがあります。

[HISTORY[
[30] [CODE(JS)@en[[[document.open]]]] は、 [[unload a document]] を呼び出すことがあります。
]HISTORY]

[32] [[unload a document]] を呼び出す場合は、それより前に [[prompt to unload a document]]
が呼び出されています。


@@ [44] [[WebDriver]] の[[スクリプト]]等が実行中の場合、...




* onunload 属性 (HTML, XHTML 1)

@@ この章は古いです


[1] [[HTML]] の [CODE(HTMLe)[[[body]]]] 要素と
[CODE(HTMLe)[[[frameset]]]] 要素の [DFN[[CODE(HTMLa)[onunload]] 属性]]は、
[[UA]] が[[窓]]または[[フレーム]]から[[文書]]を除去する時に発生する[[事象]]に関する[[内在事象]]属性です。

[2] 仕様書:
- [[HTML 4]]
-- [CODE(HTMLa)[onunload]] 属性
<IW:HTML4:"interact/scripts.html#adef-onunload">

[[#comment]]

** 代替

[3] 内在事象属性は[[表現と構造の分離]]の原則に反するので、
好ましくないと考える人もいます。また、 [[DOM 0]]
に基づく古い設計です。

代わりに、 [CODE(HTMLe)[[[script]]]] 要素などにより関連付けたスクリプト内で
[[DOM 2]] 以上の[[事象]]に関するメソッド等が利用できます。
DOM 水準2 や DOM 水準 3 の HTML 事象モジュールはこの属性に対応する
DOM の事象 [CODE(DOM)[[[unload]]]] を用意しています。

[[#comment]]

** 属性値

[4] この属性の値は [CODE(SGML)[%[[Script]]]] です。 [[SGML]]
的には [CODE(SGML)[[[CDATA]]]] です。[[スクリプト言語]]は、
[CODE(HTTP)[[[Content-Script-Type]]]] によります。

[5] この属性は省略可能です。

[[#comment]]

** 他との関係

[6] この属性に対応する DOM の事象は [CODE(DOM)[[[load]]]]
です。そちらもあわせてごらんください。

[7] 反対の意味の [CODE(HTMLa)[[[onload]]]] 属性もあります。

* 歴史

** [CODE(DOMa)@en[visibilityState]] 属性の値 [CODE(DOM)@en[unloaded]] (DOM)

[16] [CODE(DOMi)@en[[[Document]]]] [[オブジェクト]]の [CODE(DOMa)@en[[[visibilityState]]]]
[[属性]]は、[[最上位閲覧文脈]]に含まれている [CODE(DOMi)@en[[[Document]]]] が[[unload]]の過程にある時は、
[DFN[[CODE(DOM)@en[[[unloaded]]]]]] を返す[['''べきです''']]。 [SRC[>>17]]

;; [18] [CODE(DOM)@en[[[unloaded]]]] という値には対応しなくても構わない [SRC[>>17]] とされています。

;; [19] [CODE(JS)@en[[[document.hidden]]]] は[[偽]]になります。

[34] この値は現在は削除され、 [CODE[[[hidden]]]] とされています。

* メモ

[8] [CITE@en[(X)HTML5 Tracking]]
([TIME[2009-10-12 14:36:54 +09:00]] 版)
<http://html5.org/tools/web-apps-tracker?from=4104&to=4105>

[9] [CITE[Surfin’ Safari - Blog Archive » WebKit Page Cache II – The unload Event]]
([TIME[2009-10-25 15:54:55 +09:00]] 版)
<http://webkit.org/blog/516/webkit-page-cache-ii-the-unload-event/>

[10] [CITE[''''''[''''''whatwg'''''']'''''' Declarative unload data]]
( ([TIME[2012-05-08 08:28:55 +09:00]] 版))
<http://lists.whatwg.org/htdig.cgi/whatwg-whatwg.org/2012-May/035686.html>

[11] [CITE[''''''[''''''whatwg'''''']'''''' Declarative unload data]]
( ([TIME[2012-05-08 08:29:21 +09:00]] 版))
<http://lists.whatwg.org/htdig.cgi/whatwg-whatwg.org/2012-May/035706.html>

[12] [CITE[Issue 113605 - chromium - Fire event unload, onbeforeunload, pagehide - An open-source browser project to help move the web forward. - Google Project Hosting]]
([TIME[2012-05-11 17:14:26 +09:00]] 版)
<http://code.google.com/p/chromium/issues/detail?id=113605#c2>

[13] [CITE@en[Web Applications 1.0 r7318     Allow script termination dialogs to close the page too.]]
( ([TIME[2012-09-06 01:48:00 +09:00]] 版))
<http://html5.org/tools/web-apps-tracker?from=7317&to=7318>

[14] [CITE[''''''[''''''whatwg'''''']'''''' Declarative unload data]]
( ([TIME[2012-11-21 02:05:52 +09:00]] 版))
<http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2012-November/038040.html>

[15] [CITE[''''''[''''''whatwg'''''']'''''' events when navigating away before page load?]]
( ([TIME[2012-12-15 06:00:01 +09:00]] 版))
<http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2012-December/038344.html>

[20] [CITE@en[Web Applications 1.0 r8609     align 'load' and 'unload' events since they have similar quirks in practice]]
( ([TIME[2014-05-07 07:16:00 +09:00]] 版))
<http://html5.org/tools/web-apps-tracker?from=8608&to=8609>

[21] [CITE[Part2 - browsersec - Browser Security Handbook, part 2 - Browser Security Handbook - Google Project Hosting]]
([TIME[2015-03-31 16:49:11 +09:00]] 版)
<https://code.google.com/p/browsersec/wiki/Part2#Page_transition_logic>

[40] [CITE@en[Remove the storage mutex due to lack of implementation · whatwg/html@1b918cf]] ([TIME[2015-12-16 14:38:10 +09:00]] 版) <https://github.com/whatwg/html/commit/1b918cf72fcbba011f83b92ab5d1f483fb1cafa3>

[41] [CITE@en['''['''''']''' (0) Placeholder for beforeunload/unload; interaction of document.o…]]
( ([[Hixie]]著, [TIME[2007-04-21 14:30:09 +09:00]]))
<https://github.com/whatwg/html/commit/c1994a485446ab0fbfb7048df10bb081947f6405>

[42] [CITE@en['''['''giow''']''' (2) Make sure to fire the relevant events on the Window object…]]
( ([[Hixie]]著, [TIME[2009-01-21 20:34:10 +09:00]]))
<https://github.com/whatwg/html/commit/afdba55e4ae45f4c9d42c00902da1a0df7381aad>

[43] [CITE@en[Disallow *all* navigations during unload, not just cross-origin]]
([[domenic]]著, [TIME[2016-09-16 01:20:25 +09:00]])
<https://github.com/whatwg/html/commit/2a11eef8f92b0335328890dc2cb1e281d18553eb>

[45] [CITE@en[Editorial: use a flag to track recursive "unload a document"]]
([[gterzian]]著, [TIME[2018-04-21 05:16:04 +09:00]])
<https://github.com/whatwg/html/commit/d9249d166174f08fe30d843731bfeb58cfb4117c>

[61] [CITE@en[add recusiveFlag argument to unload by gterzian · Pull Request #3632 · whatwg/html]]
([TIME[2018-04-21 18:13:23 +09:00]])
<https://github.com/whatwg/html/pull/3632>

[62] [CITE@en[Editorial: modernize the style of various document-accepting algorithms]]
([[musgravejw]]著, [TIME[2018-05-03 01:20:53 +09:00]])
<https://github.com/whatwg/html/commit/8bd6fb3972aab1debd2c562b31617e7d3da2377b>

[63] [CITE@en[Various algorithms should take an explicit document argument · Issue #3638 · whatwg/html]]
([TIME[2018-05-03 11:19:48 +09:00]])
<https://github.com/whatwg/html/issues/3638>

[64] [CITE@en[Editorial: Add Document to argument list explicitly for document algorithms by musgravejw · Pull Request #3639 · whatwg/html]]
([TIME[2018-05-03 11:20:34 +09:00]])
<https://github.com/whatwg/html/pull/3639>

[65] [CITE@en[document.open() simplifications, part 1]]
([[TimothyGu]]著, [TIME[2018-08-17 07:20:44 +09:00]])
<https://github.com/whatwg/html/commit/6f769b8089a843066aa19f5991405bf4c84458b3>

[66] [CITE@en[document.open() simplifications, part 1]]
([[TimothyGu]]著, [TIME[2018-08-17 07:20:44 +09:00]])
<https://github.com/whatwg/html/commit/6f769b8089a843066aa19f5991405bf4c84458b3>

[67] [CITE@en[document.open() simplifications: realm creation, unloading, tasks removal by TimothyGu · Pull Request #3918 · whatwg/html]]
([TIME[2018-08-23 20:19:35 +09:00]])
<https://github.com/whatwg/html/pull/3918>

[68] [CITE@en[document.open() simplifications: realm creation, unloading, tasks removal by TimothyGu · Pull Request #3918 · whatwg/html]]
([TIME[2018-08-23 20:19:35 +09:00]])
<https://github.com/whatwg/html/pull/3918>

[69] [CITE@en[Disallow window.open() during unload by TimothyGu · Pull Request #3960 · whatwg/html]]
([TIME[2019-03-04 17:22:25 +09:00]])
<https://github.com/whatwg/html/pull/3960>