-
Notifications
You must be signed in to change notification settings - Fork 22.7k
/
Copy pathindex.md
101 lines (81 loc) · 2.7 KB
/
index.md
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
---
title: "Document: readyState property"
short-title: readyState
slug: Web/API/Document/readyState
page-type: web-api-instance-property
browser-compat: api.Document.readyState
---
{{APIRef("DOM")}}
The **`Document.readyState`** property describes the loading state of the {{domxref("document")}}.
When the value of this property changes, a {{domxref("Document/readystatechange_event", "readystatechange")}} event fires on the {{domxref("document")}} object.
## Value
The `readyState` of a document can be one of following:
- `loading`
- : The {{domxref("document")}} is still loading.
- `interactive`
- : The document has finished loading and the document has been parsed but sub-resources
such as scripts, images, stylesheets and frames are still loading. The state indicates that
the {{domxref("Document/DOMContentLoaded_event", "DOMContentLoaded")}} event is about to fire.
- `complete`
- : The document and all sub-resources have finished loading. The state indicates that
the {{domxref("Window/load_event", "load")}} event is about to fire.
## Examples
### Different states of readiness
```js
switch (document.readyState) {
case "loading":
// The document is loading.
break;
case "interactive": {
// The document has finished loading and we can access DOM elements.
// Sub-resources such as scripts, images, stylesheets and frames are still loading.
const span = document.createElement("span");
span.textContent = "A <span> element.";
document.body.appendChild(span);
break;
}
case "complete":
// The page is fully loaded.
console.log(
`The first CSS rule is: ${document.styleSheets[0].cssRules[0].cssText}`,
);
break;
}
```
### readystatechange as an alternative to DOMContentLoaded event
```js
// Alternative to DOMContentLoaded event
document.onreadystatechange = () => {
if (document.readyState === "interactive") {
initApplication();
}
};
```
### readystatechange as an alternative to load event
```js
// Alternative to load event
document.onreadystatechange = () => {
if (document.readyState === "complete") {
initApplication();
}
};
```
### readystatechange as event listener to insert or modify the DOM before DOMContentLoaded
```js
document.addEventListener("readystatechange", (event) => {
if (event.target.readyState === "interactive") {
initLoader();
} else if (event.target.readyState === "complete") {
initApp();
}
});
```
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- Related events:
- {{domxref("Document/readystatechange_event", "readystatechange")}}
- {{domxref("Document/DOMContentLoaded_event", "DOMContentLoaded")}}
- {{domxref("Window/load_event", "load")}}