You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 2-ui/5-loading/01-onload-ondomcontentloaded/article.md
+9-9
Original file line number
Diff line number
Diff line change
@@ -49,7 +49,7 @@ In the example the `DOMContentLoaded` handler runs when the document is loaded,
49
49
50
50
But it doesn't wait for the image to load. So `alert` shows zero sizes.
51
51
52
-
At the first sight `DOMContentLoaded` event is very simple. The DOM tree is ready -- here's the event. There are few peculiarities though.
52
+
At first sight, the`DOMContentLoaded` event is very simple. The DOM tree is ready -- here's the event. There are few peculiarities though.
53
53
54
54
### DOMContentLoaded and scripts
55
55
@@ -75,8 +75,8 @@ In the example above, we first see "Library loaded...", and then "DOM ready!" (a
75
75
76
76
```warn header="Scripts that don't block DOMContentLoaded"
77
77
There are two exceptions from this rule:
78
-
1. Scripts with `async` attribute that we'll cover [a bit later](info:script-async-defer) don't block `DOMContentLoaded`.
79
-
2. Scripts that are generated dynamically with `document.createElement('script')` and then added to page also don't block this event.
78
+
1. Scripts with the `async` attribute, that we'll cover [a bit later](info:script-async-defer), don't block `DOMContentLoaded`.
79
+
2. Scripts that are generated dynamically with `document.createElement('script')` and then added to the webpage also don't block this event.
80
80
```
81
81
82
82
### DOMContentLoaded and styles
@@ -93,7 +93,7 @@ But there's a pitfall. If we have a script after the style, then that script mus
93
93
</script>
94
94
```
95
95
96
-
The reason is that the script may want to get coordinates and other style-dependent properties of elements, like in the example above. Naturally, it has to wait for styles to load.
96
+
The reason for this is that the script may want to get coordinates and other style-dependent properties of elements, like in the example above. Naturally, it has to wait for styles to load.
97
97
98
98
As `DOMContentLoaded` waits for scripts, it now waits for styles before them as well.
99
99
@@ -217,7 +217,7 @@ if (document.readyState == 'loading') {
217
217
}
218
218
```
219
219
220
-
There's also `readystatechange` event that triggers when the state changes, so we can print all these states like this:
220
+
There's also the `readystatechange` event that triggers when the state changes, so we can print all these states like this:
221
221
222
222
```js run
223
223
// current state
@@ -272,12 +272,12 @@ The numbers in square brackets denote the approximate time of when it happens. E
272
272
273
273
Page load events:
274
274
275
-
- `DOMContentLoaded` event triggers on `document` when DOM is ready. We can apply JavaScript to elements at this stage.
275
+
- The `DOMContentLoaded` event triggers on `document` when the DOM is ready. We can apply JavaScript to elements at this stage.
276
276
- Script such as `<script>...</script>` or `<script src="..."></script>` block DOMContentLoaded, the browser waits for them to execute.
277
277
- Images and other resources may also still continue loading.
278
-
- `load` event on `window` triggers when the page and all resources are loaded. We rarely use it, because there's usually no need to wait for so long.
279
-
- `beforeunload` event on `window` triggers when the user wants to leave the page. If we cancel the event, browser asks whether the user really wants to leave (e.g we have unsaved changes).
280
-
- `unload` event on `window` triggers when the user is finally leaving, in the handler we can only do simple things that do not involve delays or asking a user. Because of that limitation, it's rarely used. We can send out a network request with `navigator.sendBeacon`.
278
+
- The `load` event on `window` triggers when the page and all resources are loaded. We rarely use it, because there's usually no need to wait for so long.
279
+
- The `beforeunload` event on `window` triggers when the user wants to leave the page. If we cancel the event, browser asks whether the user really wants to leave (e.g we have unsaved changes).
280
+
- `The unload` event on `window` triggers when the user is finally leaving, in the handler we can only do simple things that do not involve delays or asking a user. Because of that limitation, it's rarely used. We can send out a network request with `navigator.sendBeacon`.
281
281
- `document.readyState` is the current state of the document, changes can be tracked in the `readystatechange` event:
282
282
- `loading` -- the document is loading.
283
283
- `interactive` -- the document is parsed, happens at about the same time as `DOMContentLoaded`, but before it.
0 commit comments