Skip to content

Commit e630c9a

Browse files
authored
Resolve conflicts + Update article
1 parent eabc573 commit e630c9a

File tree

1 file changed

+7
-46
lines changed

1 file changed

+7
-46
lines changed

2-ui/5-loading/02-script-async-defer/article.md

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,11 @@ There are some workarounds to that. For instance, we can put a script at the bot
2929
</body>
3030
```
3131

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.
3333

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-
=======
3734
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
3935

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`.
4137

4238
## defer
4339

@@ -81,34 +77,25 @@ The following example demonstrates the second part:
8177
Let's say, we have two deferred scripts: the `long.js` and then `small.js`:
8278

8379
```html
84-
<script async src="https://javascript.info/article/script-async-defer/long.js"></script>
85-
<script async src="https://javascript.info/article/script-async-defer/small.js"></script>
80+
<script defer src="https://javascript.info/article/script-async-defer/long.js"></script>
81+
<script defer src="https://javascript.info/article/script-async-defer/small.js"></script>
8682
```
8783

8884
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.
8985

90-
<<<<<<< HEAD
91-
But the specification requres scripts to execute in the document order, so it waits for `long.js` to execute.
92-
```
93-
=======
9486
...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.
9587

9688
That may be important for cases when we need to load a JavaScript library and then a script that depends on it.
97-
>>>>>>> e074a5f825a3d10b0c1e5e82561162f75516d7e3
9889

9990
```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`.
10192
```
10293
10394
## async
10495
105-
<<<<<<< HEAD
106-
The `async` attribute means that a script is completely independant:
107-
=======
10896
The `async` attribute is somewhat like `defer`. It also makes the script non-blocking. But it has important differences in the behavior.
10997
11098
The `async` attribute means that a script is completely independent:
111-
>>>>>>> e074a5f825a3d10b0c1e5e82561162f75516d7e3
11299
113100
- The browser doesn't block on `async` scripts (like `defer`).
114101
- 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
139126
- `DOMContentLoaded` may happen both before and after `async`, no guarantees here.
140127
- 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.
141128

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:
143130

144131
```html
132+
<!-- Google Analytics is usually added like this -->
145133
<script async src="https://google-analytics.com/analytics.js"></script>
146134
```
147135

148136
## Dynamic scripts
149137

150138
There's one more important way of adding a script to the page.
151139

152-
<<<<<<< HEAD
153-
We can also create a script dynamically using JavaScript:
154-
=======
155140
We can create a script and append it to the document dynamically using JavaScript:
156-
>>>>>>> e074a5f825a3d10b0c1e5e82561162f75516d7e3
157141

158142
```js run
159143
let script = document.createElement('script');
@@ -169,15 +153,7 @@ That is:
169153
- They don't wait for anything, nothing waits for them.
170154
- The script that loads first -- runs first ("load-first" order).
171155

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-
=======
179156
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
181157

182158
In this example, `loadScript(src)` function adds a script and also sets `async` to `false`.
183159

@@ -203,11 +179,7 @@ Again, as with the `defer`, the order matters if we'd like to load a library and
203179

204180
## Summary
205181

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-
=======
209182
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
211183

212184
But there are also essential differences between them:
213185

@@ -221,20 +193,9 @@ In practice, `defer` is used for scripts that need the whole DOM and/or their re
221193
And `async` is used for independent scripts, like counters or ads. And their relative execution order does not matter.
222194

223195
```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-
=======
234196
Please note: if you're using `defer` or `async`, then user will see the the page *before* the script loads.
235197
236198
In such case, some graphical components are probably not initialized yet.
237199
238200
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.
239201
```
240-
>>>>>>> e074a5f825a3d10b0c1e5e82561162f75516d7e3

0 commit comments

Comments
 (0)