aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/web/api/document/readystate/index.html
blob: 13cf835aa7c8e23d8271c7757aea7311350f0d16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
---
title: Document.readyState
slug: Web/API/Document/readyState
tags:
  - API
  - HTML DOM
  - Reference
translation_of: Web/API/Document/readyState
---
<div>{{APIRef("DOM")}} {{ gecko_minversion_header("1.9.2") }}</div>

<h2 id="Summary" name="Summary">總覽</h2>

<p>{{ domxref("document") }} 的 <strong>Document.readyState</strong> 屬性描述文件的讀取狀態。</p>

<h3 id="數值">數值</h3>

<p>文件的 <strong>readyState</strong> 數值如下:</p>

<dl>
 <dt>loading</dt>
 <dd>{{ domxref("document") }} 正在讀取中。</dd>
 <dt>interactive</dt>
 <dd>文件已經完成讀取和解析,但是其他的子資源,如「圖片樣式層次表」,仍然在讀取。這個狀態表示 {{event("DOMContentLoaded")}} 事件已經被觸發。</dd>
 <dt>complete</dt>
 <dd>文件及子資源都完成讀取。這個狀態表示 {{event("load")}} 事件即將被觸發。</dd>
</dl>

<p>當這個屬性的數值改變時, {{event("readystatechange")}} 事件在 {{ domxref("document") }} 上觸發。</p>

<h2 id="表達式">表達式</h2>

<pre class="brush: js"><em>var string</em> = document.readyState;
</pre>

<h2 id="範例"><span>範例</span></h2>

<h3 id="不同的狀態">不同的狀態</h3>

<pre class="brush: js"><span>switch (document.readyState) {
  case "loading":
    // 文件讀取中。
    break;
  case "interactive":
    // 文件已經完成讀取。可以使用 DOM 元素。
    var span = document.createElement("span");
    span.textContent = "A &lt;span&gt; element.";
    document.body.appendChild(span);
    break;
  case "complete":
    // 頁面已經完成讀取。
    console.log("The first CSS rule is: " + document.styleSheets[0].cssRules[0].cssText);
    break;
}</span>
</pre>

<h3 id="readystatechange_替代_DOMContentLoaded">readystatechange 替代 DOMContentLoaded</h3>

<pre class="brush:js">// alternative to DOMContentLoaded event
document.onreadystatechange = function () {
  if (document.readyState == "interactive") {
    initApplication();
  }
}</pre>

<h3 id="readystatechange_替代_load">readystatechange 替代 load</h3>

<pre class="brush: js">// alternative to load event
document.onreadystatechange = function () {
  if (document.readyState == "complete") {
    initApplication();
  }
}</pre>

<h2 id="規範">規範</h2>

<table class="spectable standard-table">
 <tbody>
  <tr>
   <th scope="col">規範</th>
   <th scope="col">狀態</th>
   <th scope="col"></th>
  </tr>
  <tr>
   <td>{{SpecName("HTML WHATWG", "#current-document-readiness", "Document readiness")}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName("HTML5.1", "#current-document-readiness", "Document readiness")}}</td>
   <td>{{Spec2('HTML5.1')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName("HTML5 W3C", "#current-document-readiness", "Document readiness")}}</td>
   <td>{{Spec2('HTML5 W3C')}}</td>
   <td>Initial specification.</td>
  </tr>
 </tbody>
</table>

<h2 id="參見">參見</h2>

<ul>
 <li>{{event("readystatechange")}} event</li>
 <li>{{event("DOMContentLoaded")}} event</li>
 <li>{{event("load")}} event</li>
</ul>