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/02-script-async-defer/article.md
+9-11Lines changed: 9 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -29,11 +29,11 @@ There are some workarounds to that. For instance, we can put a script at the bot
29
29
</body>
30
30
```
31
31
32
-
But this solution is far from perfect. For example, the browser actually notices the script (and can start downloading it) only after it downloaded the full HTML document. For long HTML documents, that may be a noticeable delay.
32
+
But this solution is far from perfect. For example, the browser notices the script (and can start downloading it) only after it downloaded the full HTML document. For long HTML documents, that may be a noticeable delay.
33
33
34
-
Such things are invisible for people using very fast connections, but many people in the world still have slower internet speeds and far-from-perfect mobile connectivity.
34
+
Such things are invisible for people using very fast connections, but many people in the world still have slower internet speeds and use far-from-perfect mobile internet.
35
35
36
-
Luckily, there are two `<script>` attributes that solve the problem for us: `defer` and `async`
36
+
Luckily, there are two `<script>` attributes that solve the problem for us: `defer` and `async`.
37
37
38
38
## defer
39
39
@@ -86,7 +86,7 @@ But the specification requires scripts to execute in the document order, so it w
86
86
```
87
87
88
88
```smart header="The `defer` attribute is only for external scripts"
89
-
The `defer` attribute is ignored if the script has no `src`.
89
+
The `defer` attribute is ignored if the `<script>` tag has no `src`.
90
90
```
91
91
92
92
@@ -120,16 +120,17 @@ So, if we have several `async` scripts, they may execute in any order. Whatever
120
120
2.`DOMContentLoaded` may happen both before and after `async`, no guarantees here.
121
121
3. Async scripts don't wait for each other. A smaller script `small.js` goes second, but probably loads before `long.js`, so runs first. That's called a "load-first" order.
122
122
123
-
Async scripts are great when we integrate an independent third-party script into the page: counters, ads and so on.
123
+
Async scripts are great when we integrate an independent third-party script into the page: counters, ads and so on, as they don't depend on our scripts, and our scripts shouldn't wait for them:
124
124
125
125
```html
126
+
<!-- Google Analytics is usually added like this -->
We can also create a script dynamically using JavaScript:
133
+
We can also add a script dynamically using JavaScript:
133
134
134
135
```js run
135
136
let script =document.createElement('script');
@@ -145,7 +146,7 @@ That is:
145
146
- They don't wait for anything, nothing waits for them.
146
147
- The script that loads first -- runs first ("load-first" order).
147
148
148
-
We can change the load-first order into the document order by explicitly setting `async` to `false`:
149
+
We can change the load-first order into the document order (just like regular scripts) by explicitly setting `async` property to `false`:
149
150
150
151
```js run
151
152
let script =document.createElement('script');
@@ -191,7 +192,4 @@ Please note that if you're using `defer`, then the page is visible before the sc
191
192
192
193
So, buttons should be disabled by CSS or by other means, to let the user
193
194
194
-
In practice, `defer` is used for scripts that need DOM and/or their relative execution order is important.
195
-
196
-
197
-
So `async` is used for independent scripts, like counters or ads, that don't need to access page content. And their relative execution order does not matter.
195
+
In practice, `defer` is used for scripts that need the whole DOM and/or their relative execution order is important. And `async` is used for independent scripts, like counters or ads. And their relative execution order does not matter.
0 commit comments