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
+7-46Lines changed: 7 additions & 46 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,15 +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
-
<<<<<<< HEAD
35
-
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.
36
-
=======
37
34
Such things are invisible for people using very fast connections, but many people in the world still have slow internet speeds and use a far-from-perfect mobile internet connection.
38
-
>>>>>>> e074a5f825a3d10b0c1e5e82561162f75516d7e3
39
35
40
-
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`.
41
37
42
38
## defer
43
39
@@ -81,34 +77,25 @@ The following example demonstrates the second part:
81
77
Let's say, we have two deferred scripts: the `long.js` and then `small.js`:
Browsers scan the page for scripts and download them in parallel, to improve performance. So in the example above both scripts download in parallel. The `small.js` probably finishes first.
89
85
90
-
<<<<<<< HEAD
91
-
But the specification requres scripts to execute in the document order, so it waits for `long.js` to execute.
92
-
```
93
-
=======
94
86
...But the `defer` atribute, besides telling the browser "not to block", ensures that the relative order is kept. So even though `small.js` loads first, it still waits and runs after `long.js` executes.
95
87
96
88
That may be important for cases when we need to load a JavaScript library and then a script that depends on it.
97
-
>>>>>>> e074a5f825a3d10b0c1e5e82561162f75516d7e3
98
89
99
90
```smart header="The `defer` attribute is only for external scripts"
100
-
The `defer` attribute is ignored if the script has no `src`.
91
+
The `defer` attribute is ignored if the `<script>` tag has no `src`.
101
92
```
102
93
103
94
## async
104
95
105
-
<<<<<<< HEAD
106
-
The `async` attribute means that a script is completely independant:
107
-
=======
108
96
The `async` attribute is somewhat like `defer`. It also makes the script non-blocking. But it has important differences in the behavior.
109
97
110
98
The `async` attribute means that a script is completely independent:
111
-
>>>>>>> e074a5f825a3d10b0c1e5e82561162f75516d7e3
112
99
113
100
- The browser doesn't block on `async` scripts (like `defer`).
114
101
- Other scripts don't wait for `async` scripts, and `async` scripts don't wait for them.
@@ -139,21 +126,18 @@ They don't wait for each other. Whatever loads first (probably `small.js`) -- ru
139
126
-`DOMContentLoaded` may happen both before and after `async`, no guarantees here.
140
127
- A smaller script `small.js` goes second, but probably loads before `long.js`, so `small.js` runs first. Although, it might be that `long.js` loads first, if cached, then it runs first. In other words, async scripts run in the "load-first" order.
141
128
142
-
Async scripts are great when we integrate an independant third-party script into the page: counters, ads and so on.
129
+
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:
143
130
144
131
```html
132
+
<!-- Google Analytics is usually added like this -->
There's one more important way of adding a script to the page.
151
139
152
-
<<<<<<< HEAD
153
-
We can also create a script dynamically using JavaScript:
154
-
=======
155
140
We can create a script and append it to the document dynamically using JavaScript:
156
-
>>>>>>> e074a5f825a3d10b0c1e5e82561162f75516d7e3
157
141
158
142
```js run
159
143
let script =document.createElement('script');
@@ -169,15 +153,7 @@ That is:
169
153
- They don't wait for anything, nothing waits for them.
170
154
- The script that loads first -- runs first ("load-first" order).
171
155
172
-
<<<<<<< HEAD
173
-
<<<<<<< HEAD
174
-
We can change the load-first order into the document order by explicitly setting `async` to `false`:
175
-
=======
176
-
This can be changed if we explicitly set `script.async=true`. Then scripts will be executed in the document order, just like `defer`.
177
-
>>>>>>> e074a5f825a3d10b0c1e5e82561162f75516d7e3
178
-
=======
179
156
This can be changed if we explicitly set `script.async=false`. Then scripts will be executed in the document order, just like `defer`.
180
-
>>>>>>> f489145731a45df6e369a3c063e52250f3f0061d
181
157
182
158
In this example, `loadScript(src)` function adds a script and also sets `async` to `false`.
183
159
@@ -203,11 +179,7 @@ Again, as with the `defer`, the order matters if we'd like to load a library and
203
179
204
180
## Summary
205
181
206
-
<<<<<<< HEAD
207
-
Both `async` and `defer` have one common thing: they don't block page rendering. So the user can read page content and get acquanted with the page immediately.
208
-
=======
209
182
Both `async` and `defer` have one common thing: downloading of such scripts doesn't block page rendering. So the user can read page content and get acquainted with the page immediately.
210
-
>>>>>>> e074a5f825a3d10b0c1e5e82561162f75516d7e3
211
183
212
184
But there are also essential differences between them:
213
185
@@ -221,20 +193,9 @@ In practice, `defer` is used for scripts that need the whole DOM and/or their re
221
193
And `async` is used for independent scripts, like counters or ads. And their relative execution order does not matter.
222
194
223
195
```warn header="Page without scripts should be usable"
224
-
<<<<<<< HEAD
225
-
Please note that if you're using `defer`, then the page is visible before the script loads and enables all the graphical components.
226
-
227
-
So, buttons should be disabled by CSS or by other means, to let the user
228
-
229
-
In practice, `defer` is used for scripts that need DOM and/or their relative execution order is important.
230
-
231
-
232
-
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.
233
-
=======
234
196
Please note: if you're using `defer` or `async`, then user will see the the page *before* the script loads.
235
197
236
198
In such case, some graphical components are probably not initialized yet.
237
199
238
200
Don't forget to put "loading" indication and disable buttons that aren't functional yet. Let the user clearly see what he can do on the page, and what's still getting ready.
0 commit comments