[7] [CODE(DOMi)@en[[[Document]]]] [[オブジェクト]]の [DFN[[CODE(DOMa)@en[[[readyState]]]] [[属性]]]]は、
[[現在文書準備度]]を表します。

* 仕様書

[REFS[
- [8] [[Web Applications 1.0]] ([[HTML5]])
-- [10] '''<http://www.whatwg.org/specs/web-apps/current-work/complete.html#dom-document-readystate>'''
--- [CODE(JS)@en[[[document.readyState]]]]
-- [11] '''<http://www.whatwg.org/specs/web-apps/current-work/complete.html#current-document-readiness>'''
--- [[現在文書準備度]]
-- [16] <http://www.whatwg.org/specs/web-apps/current-work/complete.html#dom-xmldocumentloader-load>
--- [CODE(JS)@en[[[document.load]]]]
-- [21] <http://www.whatwg.org/specs/web-apps/current-work/complete.html#the-end>
--- [[構文解析停止]]
]REFS]

* 取得器

[12] [CODE(JS)@en[[[document.readyState]]]] [[属性]]は[[文書]]の[[現在文書準備度]]を返さなければ[['''なりません''']]
[SRC[>>10]]。

* 現在文書準備度

[9] [[文書]]は「[DFN[[RUBYB[[[現在文書準備度]]]@en[current document readiness]]]]」という[[状態]]を持っています
[SRC[>>11]]。

[6] [[構文解析]]や [[XSLT]] の処理を行う場合、[[現在文書準備度]]は、 
[CODE(DOM)@en[[[loading]]]] → [CODE(DOM)@en[[[interactive]]]]
→ [CODE(DOM)@en[[[complete]]]] と変化します。そうでない場合は、
最初から [CODE(DOM)@en[[[complete]]]] となります。

;; [24] この3つの値は、 [[Web IDL]] の [CODE(IDL)@en[[[enum]]]] である
[DFN[[CODE(IDL)@en[[[DocumentReadyState]]]]]] です。

[23] [CODE(JS)@en[[[document.open]]]] と [CODE(JS)@en[[[document.load]]]]
では、[[現在文書準備度]]が [CODE(DOM)@en[[[loading]]]] に戻ります。

[25] 詳しくは、次のように変化します。

[FIG(list)[
- [13] [[文書]]が作られたとき、[[HTML構文解析器]]または[[XML構文解析器]]と関連付けられていれば、[[現在文書準備度]]は [DFN[[CODE(DOM)@en[[[loading]]]]]] に設定されなければ[['''なりません''']]
[SRC[>>11]]。
- [14] [[文書]]が作られたとき、 >>13 以外であれば、[[現在文書準備度]]は [DFN[[CODE(DOM)@en[[[complete]]]]]]
に設定されなければ[['''なりません''']] [SRC[>>11]]。
- [17] [CODE(JS)@en[[[document.load]]]] [[メソッド]]は読み込みの開始前に[[現在文書準備度]]を [CODE(DOM)@en[[[loading]]]] に設定します [SRC[>>16]]。
- [18] [CODE(JS)@en[[[document.load]]]] [[メソッド]]は読み込みの完了後で[[文書]]に変更が反映される直前に[[現在文書準備度]]を [CODE(DOM)@en[[[complete]]]] に設定します [SRC[>>16]]。
- [3] [CODE(JS)@en[[[document.open]]]] は[[現在文書準備度]]を
[CODE(DOM)@en[[[loading]]]] に設定します。
- [20] [[HTML構文解析器]]は[[構文解析停止]]の最初で[[現在文書準備度]]を 
[DFN[[CODE(DOM)@en[[[interactive]]]]]] に設定します [SRC[>>21]]。
- [22] [[HTML構文解析器]]は [CODE(DOMe)@en[[[load]]]] [[事象]]の[[発火]]の直前に[[現在文書準備度]]を
[CODE(DOM)@en[[[complete]]]] に設定します [SRC[>>21]]。
- [1] [[abort a parser]] は[[現在文書準備度]]を [CODE(DOM)@en[[[interactive]]]] へ、更に
[CODE(DOM)@en[[[complete]]]] へと設定します。
- [5] [[XSLT]] の処理は[[現在文書準備度]]を [CODE(DOM)@en[[[interactive]]]] へ、更に
[CODE(DOM)@en[[[complete]]]] へと設定します。
]FIG]

[15] [[現在文書準備度]]が設定された時には、その [CODE(DOMi)@en[[[Document]]]]
[[オブジェクト]]において [CODE(DOMe)@en[[[readystatechange]]]] [[単純事象を発火]]しなければ[['''なりません''']]
[SRC[>>11]]。

[19] 
[[現在文書準備度]]は[[履歴探索]]の過程で [CODE(DOMe)@en[[[pageshow]]]] [[事象]]や
[CODE(DOMe)@en[[[popstate]]]] [[事象]]が[[発火]]されるかに影響します。

;;
[REFS[
- <http://www.whatwg.org/specs/web-apps/current-work/complete.html#history-traversal>
]REFS]

* 歴史

[2] [CITE@ja[Dynamic HTML(クライアントサイドスクリプトやIE固有のプログラム要素も含む)]] ([TIME[2007-03-18 16:34:07 +09:00]] 版) <http://www.rody.dev-asp.net/memo/dhtml/index.html>

>絶対にそうだとは言えないが、調べたところでは以下のような関係だった。
=子ウィンドウを持つウィンドウは全ての子ウィンドウのreadyStateが"complete"にならないと、自ウィンドウのreadyStateは"complete"にならない。また、正確にはreadyStateが"complete"でdocument.onReadyStateChangeイベントを抜けた時に、完全に"complete"したとみなす。
= window.onloadイベントは自ウィンドウのreadyStateが"complete"になった後、発生する(正確な判断は1と同様)。
=兄弟ウィンドウ同士は非同期で動作する。
=(重要)1及び2からウィンドウが"complete"する順番は子ウィンドウから正しく"complete"する事になるが、各ウィンドウのonloadイベントに関しては必ずしも同期しているとは限らない。親ウィンドウのonloadが子ウィンドウのonloadよりも先に走る可能性もタイミングによっては有り得る。
= 他のファイルをインクルードしているものに関しては分からないが、 readyStateが"complete"になるのはhtmlタグ内のドキュメントが読み込み終わった時ではなくて、そのファイル全体のソースが読み込み終わった時に発生しているっぽい(どっちにしろhtmlタグ内に全て書いていれば関係ないが)。
>
補足:もし、子ウィンドウのロードを非同期で(同時に)行い、各ウィンドウの初期処理を正確に順番通りに行いたいためにトップウィンドウのonReadyStateChangeイベント、もしくはonloadイベントで子ウィンドウの初期処理を順番に行うのであれば、子ウィンドウのonReadyStateChangeイベントもしくはonloadイベントに初期処理を入れるべきではない。トップウィンドウからの初期処理関数呼び出しと、各子ウィンドウのonloadイベントに初期処理が記述されている場合、どちらの初期処理が先に走るかは不定。よって、もう片方の初期処理が完了している事を前提でコーディングすると、処理が走るタイミングによっては未初期化でエラーが発生するかも。 

[4]
[CITE[document.readyState - 素人がプログラミングを勉強するブログ]] ([TIME[2009-02-08 18:12:23 +09:00]] 版) <http://d.hatena.ne.jp/javascripter/20090125/1232880728>

[37] [CITE@en[Web Applications 1.0 r5501     document.readyState should be updated after csalling document.open().]]
( ([TIME[2010-09-26 05:45:00 +09:00]] 版))
<http://html5.org/tools/web-apps-tracker?from=5500&to=5501>

[43] [CITE@en[HTML5 Tracker]]
([TIME[2011-10-15 15:27:33 +09:00]] 版)
<http://html5.org/tools/web-apps-tracker?from=6688&to=6689>

[44] [CITE@en[Web Applications 1.0 r6882     Make the readyState change, the 'readystatechange' event, and the 'load' event all happen synchronously in the same task so that you can check readyState to know whether you still have to attach an 'onload' event handler.]]
( ([TIME[2012-01-10 09:08:00 +09:00]] 版))
<http://html5.org/tools/web-apps-tracker?from=6881&to=6882>

[45] [CITE[''''''[''''''whatwg'''''']'''''' Readiness of script-created documents]]
( ([TIME[2012-01-14 12:23:16 +09:00]] 版))
<http://lists.whatwg.org/htdig.cgi/whatwg-whatwg.org/2012-January/034363.html>

[46] [CITE[IRC logs: freenode / #whatwg / 20120313]]
( ([TIME[2012-03-17 20:31:54 +09:00]] 版))
<http://krijnhoetmer.nl/irc-logs/whatwg/20120313>

[47] [CITE[''''''[''''''whatwg'''''']'''''' Readiness of script-created documents]]
( ([TIME[2012-06-12 08:10:52 +09:00]] 版))
<http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2012-June/036356.html>

[48] [CITE@en[Web Applications 1.0 r7169     Make 'abort the parser' fire some readystatechange events for more consistency. Slightly expand on how little XSLT/HTML interactions are defined. Rename 'cookie-free' to 'cookie-averse' for clarity.]]
( ([TIME[2012-07-11 04:15:00 +09:00]] 版))
<http://html5.org/tools/web-apps-tracker?from=7168&to=7169>

[49] [CITE[''''''[''''''whatwg'''''']'''''' Proposal for readyState behavior]]
( ([TIME[2012-07-11 07:30:22 +09:00]] 版))
<http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2012-July/036592.html>

[50] [CITE[''''''[''''''whatwg'''''']'''''' DOMContentLoaded, load and current document readiness]]
( ([TIME[2012-08-30 01:12:43 +09:00]] 版))
<http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2012-August/037045.html>

[51] [CITE[''''''[''''''whatwg'''''']'''''' Proposal for readyState behavior]]
( ([TIME[2012-08-30 01:12:43 +09:00]] 版))
<http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2012-August/037042.html>

[52] [CITE[''''''[''''''whatwg'''''']'''''' Readiness of script-created documents]]
( ([TIME[2012-08-30 01:12:43 +09:00]] 版))
<http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2012-August/037040.html>

[53] [CITE@en[Web Applications 1.0 r7299     Try to make aborting parsing and regular ending of parsing slightly more similar. Also, emphasise the importance of stress in the definition of <em>. (Sorry for the mixed checkin.)]]
( ([TIME[2012-08-31 02:46:00 +09:00]] 版))
<http://html5.org/tools/web-apps-tracker?from=7298&to=7299>

[54] [CITE@en[Web Applications 1.0 r7781 Make document.readyState explicitly an enum, not that that has any real effect.]]
( ([TIME[2013-03-30 03:13:00 +09:00]] 版))
<http://html5.org/tools/web-apps-tracker?from=7780&to=7781>
