Skip to content

Sync with upstream @ 273e47b7 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 590 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
590 commits
Select commit Hold shift + click to select a range
c8e7222
fix
iliakan Mar 9, 2019
fa8ffbe
event options
iliakan Mar 9, 2019
d912bce
minor
iliakan Mar 9, 2019
364e707
promisify
iliakan Mar 9, 2019
c0ff7ca
Replace old, broken chapter link with new chapters
georgesolomos Mar 10, 2019
f04f269
Fix Typo
yifeiyin Mar 10, 2019
9cc6d70
Corrected grammatical errors, refined prose style
Mar 11, 2019
32bf3fd
idnexeeddb draft, microqueue fixes
iliakan Mar 11, 2019
37e0b0e
Fix typo
rizkyzhang Mar 12, 2019
fdda111
Fix some typo
rizkyzhang Mar 12, 2019
e60bb12
indexeddb
iliakan Mar 12, 2019
c7683b6
Added missing semicolon
RazvanBugoi Mar 13, 2019
5ca83ba
refactor
iliakan Mar 13, 2019
b9bfba0
minor
iliakan Mar 13, 2019
29292c1
minor
iliakan Mar 13, 2019
943c971
minor
iliakan Mar 13, 2019
00b356c
refactor xhr
iliakan Mar 14, 2019
e882e72
fix
iliakan Mar 14, 2019
c7645b8
fix
iliakan Mar 14, 2019
93b87d3
fix
iliakan Mar 14, 2019
d5b9b89
fix
iliakan Mar 14, 2019
5daae78
fix
iliakan Mar 14, 2019
9fb235a
fix
iliakan Mar 14, 2019
5de1662
fix
iliakan Mar 14, 2019
440f408
fix
iliakan Mar 14, 2019
43626f7
Update article.md
Mar 14, 2019
66fa857
fetch draft
iliakan Mar 15, 2019
61ed067
Added a space after "for"
yifeiyin Mar 16, 2019
7f9851e
Merge pull request #1 from yifeiyin/master
yifeiyin Mar 16, 2019
37241b6
fetch
iliakan Mar 16, 2019
7f9a1e2
up
iliakan Mar 16, 2019
b1db8c0
Update article.md
ofcRS Mar 17, 2019
ac74268
Update article.md
ofcRS Mar 17, 2019
973f97c
binary draft
iliakan Mar 17, 2019
7d83a1a
Update article.md
Senseinvader Mar 17, 2019
ef81c0e
Update article.md
Senseinvader Mar 17, 2019
68d1ac1
drafts
iliakan Mar 18, 2019
31f83a5
network draft
iliakan Mar 18, 2019
1a54a9e
Fix Typo
iamplugged Mar 19, 2019
c47a379
a bug (Maximum call stack size exceeded)
helloendoftheworld Mar 19, 2019
4ffd79e
up
iliakan Mar 20, 2019
b716980
minor
iliakan Mar 20, 2019
3fea520
minor
iliakan Mar 20, 2019
2bcbbfc
up
iliakan Mar 20, 2019
855f67f
up
iliakan Mar 20, 2019
a420755
Merge pull request #837 from maurodibert/patch-40
iliakan Mar 20, 2019
caaaf0d
Merge pull request #863 from iamplugged/patch-1
iliakan Mar 20, 2019
bb33dcd
Merge pull request #861 from SherifGhoz/SherifGhoz-patch-1
iliakan Mar 20, 2019
4d35fe5
Merge pull request #859 from Senseinvader/patch-2
iliakan Mar 20, 2019
f67d7ad
Merge pull request #858 from Senseinvader/patch-1
iliakan Mar 20, 2019
5791f59
Merge pull request #857 from ofcRS/patch-2
iliakan Mar 20, 2019
fa5ace4
Merge pull request #856 from ofcRS/patch-1
iliakan Mar 20, 2019
1ae4d45
Merge pull request #854 from Baelx/patch-1
iliakan Mar 20, 2019
66b5039
Merge pull request #853 from RazvanBugoi/master
iliakan Mar 20, 2019
26839ee
Merge pull request #850 from James868/patch-2
iliakan Mar 20, 2019
7f4040a
Merge pull request #849 from James868/patch-1
iliakan Mar 20, 2019
4dcb97c
Merge pull request #848 from yifeiyin/patch-1
iliakan Mar 20, 2019
229a18f
Merge pull request #847 from georgesolomos/fix-broken-link
iliakan Mar 20, 2019
482dd2b
Merge pull request #846 from amslezak/patch-1
iliakan Mar 20, 2019
75dd9c8
Merge pull request #845 from vitorfurlin/patch-1
iliakan Mar 20, 2019
0166f1d
Merge pull request #841 from aiyayayaya/patch-1
iliakan Mar 20, 2019
cdb0a69
Merge pull request #840 from carlinmack/patch-1
iliakan Mar 20, 2019
5ab51a3
Merge pull request #839 from agdillon/master
iliakan Mar 20, 2019
1985d1c
Merge pull request #844 from aiyayayaya/patch-2
iliakan Mar 20, 2019
e42e8d9
Update article.md
mb1047 Mar 20, 2019
3b06ab3
ws draft
iliakan Mar 20, 2019
37f50d8
websocket
iliakan Mar 21, 2019
591b5f7
Update article.md
avinashsinghdhillon Mar 22, 2019
8998389
Update article.md
odykyi Mar 22, 2019
ffb9ce2
up
iliakan Mar 22, 2019
8489679
minor
iliakan Mar 22, 2019
e202ec2
edits
iliakan Mar 22, 2019
439cf4c
improvements
iliakan Mar 22, 2019
b8d63d3
improvements
iliakan Mar 22, 2019
50bee36
improvements
iliakan Mar 22, 2019
92f7f1b
fixes
iliakan Mar 22, 2019
706b1f2
fixes
iliakan Mar 22, 2019
db55cf7
Update to improve clarity and avoid confusion.
gargakshat Mar 23, 2019
323de66
Fix typo
rizkyzhang Mar 26, 2019
69aaf57
Fix typos
jessuni Mar 26, 2019
da51ce5
Fix English typo
ekqvist Mar 26, 2019
0873d43
up
iliakan Mar 26, 2019
260bca1
Update article.md
maurodibert Mar 28, 2019
1c8e98c
The previous link was DEPRECATED
EdThePro101 Mar 28, 2019
cae79b5
Fix function parameter
vasilevx Mar 29, 2019
65671ab
draft
iliakan Mar 30, 2019
9c3ac13
draft
iliakan Mar 30, 2019
8de6fa6
draft
iliakan Mar 30, 2019
a9fb779
Small Grammatical Error
LordLexLutheran Mar 30, 2019
79324d0
draft
iliakan Mar 30, 2019
304d578
up
iliakan Mar 31, 2019
7c3df46
Update article.md
overken Apr 1, 2019
da9d344
Update article.md
overken Apr 1, 2019
e4a1148
Merge pull request #866 from mb1047/patch-1
iliakan Apr 2, 2019
69c7491
Merge pull request #867 from avinashsinghdhillon/patch-1
iliakan Apr 2, 2019
956be1b
Merge pull request #868 from odykyi/patch-1
iliakan Apr 2, 2019
64ffd43
Merge pull request #886 from overken/patch-2
iliakan Apr 2, 2019
e9be10d
Merge pull request #885 from overken/patch-1
iliakan Apr 2, 2019
8925505
Merge pull request #882 from LordLexLutheran/patch-1
iliakan Apr 2, 2019
7fa1bf7
Merge pull request #880 from vasilevx/patch-1
iliakan Apr 2, 2019
0535487
Merge pull request #878 from Edwin-Pratt/patch-1
iliakan Apr 2, 2019
a8be18d
Merge pull request #876 from maurodibert/patch-44
iliakan Apr 2, 2019
8006d3c
Merge pull request #874 from Supernaiivi/typos
iliakan Apr 2, 2019
6d11147
Merge pull request #873 from jessuni/patch-2
iliakan Apr 2, 2019
184f7ae
Merge pull request #872 from James868/patch-3
iliakan Apr 2, 2019
6fb4aab
components
iliakan Apr 2, 2019
e22e971
Merge branch 'refactor'
iliakan Apr 2, 2019
3dbe87a
minor
iliakan Apr 2, 2019
9dff4c0
Merge pull request #870 from gargakshat/patch-1
iliakan Apr 2, 2019
911f783
minor
iliakan Apr 2, 2019
1932bfb
Typo in article.md
srogalsky8 Apr 2, 2019
94d30ce
Fix another typo
srogalsky8 Apr 2, 2019
85a0449
Merge pull request #1 from srogalsky8/master
srogalsky8 Apr 2, 2019
f19495e
Update article.md
overken Apr 3, 2019
a649079
More typos
srogalsky8 Apr 3, 2019
374bd26
Merge pull request #887 from srogalsky8/patch-1
iliakan Apr 3, 2019
2c0c69e
Merge pull request #888 from overken/patch-3
iliakan Apr 3, 2019
d7d1c17
todo
iliakan Apr 3, 2019
b5a531a
change it's to its
dagolinuxoid Apr 4, 2019
34fd31c
Update article.md
jackpickus Apr 5, 2019
2a39ef8
minor
iliakan Apr 5, 2019
8f13d61
Sketch editor update (no important image changes)
iliakan Apr 5, 2019
e3e4502
Mistake in naming the property name.
amolpatravali97 Apr 8, 2019
0789e38
Update README.md
iliakan Apr 8, 2019
798840e
promise result exact string match
holub008 Apr 8, 2019
b4053e1
Fix typo
jtlx Apr 8, 2019
9ad3206
typo fix
AdamDemirel Apr 8, 2019
1411881
Fix typo of 'Fetch: Abort'
Apr 9, 2019
18e30f1
Update article.md
Hormozdi Apr 9, 2019
1728f97
Fixing the syntax typo in prompt
atulsm Apr 9, 2019
d25d29c
Update article.md
atulsm Apr 9, 2019
16deb3c
General grammar/typo changes
elitree Apr 9, 2019
dfcba54
Merge pull request #889 from dagolinuxoid/fix-typo
iliakan Apr 9, 2019
6761dac
Merge pull request #901 from elitree/master
iliakan Apr 9, 2019
88c371a
Merge pull request #900 from atulsm/master
iliakan Apr 9, 2019
6190863
Merge pull request #899 from Hormozdi/master
iliakan Apr 9, 2019
8c6eb6e
Merge pull request #898 from liufei/master
iliakan Apr 9, 2019
b581d2c
Merge pull request #897 from brasscapon/patch-1
iliakan Apr 9, 2019
bcf8239
Merge pull request #896 from jtlx/patch-1
iliakan Apr 9, 2019
67ed0e0
Merge pull request #895 from holub008/master
iliakan Apr 9, 2019
1349e70
Merge pull request #894 from amolpatravali97/patch-1
iliakan Apr 9, 2019
c9631a1
Merge pull request #890 from jackpickus/patch-1
iliakan Apr 9, 2019
cf8218a
Update README.md
iliakan Apr 10, 2019
f979c09
Update README.md
iliakan Apr 10, 2019
7a38c1d
Update README.md
iliakan Apr 10, 2019
b7c77df
Update README.md
iliakan Apr 10, 2019
a72ee1d
Update README.md
iliakan Apr 10, 2019
4f925a1
Readme
iliakan Apr 10, 2019
222c52f
minor
iliakan Apr 10, 2019
726d7d5
fix
iliakan Apr 10, 2019
23d77ee
fix
iliakan Apr 10, 2019
c186ed8
Update README.md
iliakan Apr 10, 2019
c4b2a2f
Update README.md
iliakan Apr 10, 2019
7b6b40e
Type in 22-regexp-sticky/article.md
HaydenLobry Apr 11, 2019
7a9a83e
typos and improved wording
Apr 11, 2019
b362191
style
iliakan Apr 11, 2019
326bb84
Update style-guide.md
iliakan Apr 11, 2019
892c56a
up
iliakan Apr 11, 2019
3b97c0a
Change "water" to "value" parameter in sample
vasilevx Apr 11, 2019
271129e
Change link destination
kolumb Apr 13, 2019
24d132b
Minor grammatical typo fix
dragonwocky Apr 14, 2019
6d0fe48
Merge pull request #903 from HaydenLobry/patch-1
iliakan Apr 14, 2019
dda595e
Merge pull request #904 from tobycheese/typos_wording
iliakan Apr 14, 2019
7c4e330
Merge pull request #906 from vasilevx/patch-1
iliakan Apr 14, 2019
5c97087
Merge pull request #907 from kolumb/patch-2
iliakan Apr 14, 2019
1d19bcb
Merge pull request #909 from dragonwocky/patch-1
iliakan Apr 14, 2019
4966420
Just remove a strange symbol
oshliaer Apr 14, 2019
7e59d4b
translate
iliakan Apr 14, 2019
d886f55
fix function signature
Apr 14, 2019
bfa672d
fix function signature in view
Apr 14, 2019
0254608
Merge pull request #911 from oshliaer/9_03_article_strange-symbol
iliakan Apr 14, 2019
2b985ee
Merge pull request #912 from Azatik1000/master
iliakan Apr 14, 2019
0a95d04
closes #908
iliakan Apr 15, 2019
effc64a
Update article.md
abdullah-manzoor-ba Apr 15, 2019
dc2da30
Update arguments order of `then`
neoarma Apr 15, 2019
5856120
Update article.md
abdullah-manzoor-ba Apr 15, 2019
c113522
smoothed over wording
dragonwocky Apr 16, 2019
52f899a
inheret -> inherit
dragonwocky Apr 16, 2019
8a9d97a
itselfs -> itself
dragonwocky Apr 16, 2019
81a37be
typos
dragonwocky Apr 16, 2019
4a29486
minor
iliakan Apr 16, 2019
d50fff0
Update TRANSLATION.md
iliakan Apr 16, 2019
e06c906
Update article.md
odax Apr 16, 2019
3f753a1
Merge pull request #917 from dragonwocky/patch-2
iliakan Apr 17, 2019
56b954a
Merge pull request #918 from odax/patch-1
iliakan Apr 17, 2019
f81fd00
Merge pull request #916 from misterabd/patch-2
iliakan Apr 17, 2019
27db301
Update article.md
iliakan Apr 17, 2019
e73bbce
Merge pull request #915 from neoarma/patch-1
iliakan Apr 17, 2019
eb43d82
Merge pull request #914 from misterabd/patch-1
iliakan Apr 17, 2019
a9d2176
popup
iliakan Apr 17, 2019
30f1dc4
abort
iliakan Apr 17, 2019
bc845fd
make wording clearer
dragonwocky Apr 17, 2019
92f2c7b
Update article.md
1van0v Apr 17, 2019
a3c66e0
Merge pull request #920 from 1van0v/patch-1
iliakan Apr 17, 2019
1f19fd8
Merge pull request #919 from dragonwocky/patch-3
iliakan Apr 17, 2019
ef91e09
Fixed syntax error / typo
candh Apr 17, 2019
17223e6
Update index.md
JohnMcGuinness Apr 17, 2019
a64fb26
fix
iliakan Apr 18, 2019
f776557
import typo
dragonwocky Apr 18, 2019
a63d623
vise -> vice
dragonwocky Apr 18, 2019
922170b
thee -> the
dragonwocky Apr 18, 2019
4f6864e
fix typo in article title
tadjik1 Apr 19, 2019
b6f0a67
Merge pull request #925 from tadjik1/patch-1
iliakan Apr 19, 2019
811491d
Update README.md
iliakan Apr 19, 2019
c62633c
Update README.md
iliakan Apr 19, 2019
b79ba7d
Update README.md
iliakan Apr 19, 2019
75c51cf
Update README.md
iliakan Apr 19, 2019
f3ea976
Update README.md
iliakan Apr 19, 2019
f6a7c12
Update README.md
iliakan Apr 19, 2019
becdb24
Update README.md
iliakan Apr 19, 2019
08fa870
Update README.md
iliakan Apr 19, 2019
df0c224
Update README.md
iliakan Apr 19, 2019
79f0463
Update README.md
iliakan Apr 19, 2019
557d170
Update solution.md
him2312 Apr 20, 2019
0697954
minor
iliakan Apr 20, 2019
65e805c
Minor grammar fix
mihansweatpants Apr 20, 2019
42ae87a
Fix typo
mihansweatpants Apr 20, 2019
f363b92
Fixed typo
alaabouayed Apr 20, 2019
3dbd493
Merge pull request #921 from candh/patch-1
iliakan Apr 21, 2019
de9f268
Merge pull request #922 from JohnMcGuinness/patch-1
iliakan Apr 21, 2019
333a00b
Merge pull request #924 from dragonwocky/patch-4
iliakan Apr 21, 2019
77581bc
Merge pull request #927 from him2312/patch-1
iliakan Apr 21, 2019
7b884bf
Merge pull request #928 from mihansweatpants/patch-1
iliakan Apr 21, 2019
0a2b817
Merge pull request #929 from mihansweatpants/patch-2
iliakan Apr 21, 2019
be9f48c
Merge pull request #930 from alaabouayed/patch-1
iliakan Apr 21, 2019
b8eb04d
classes: remove patterns
iliakan Apr 21, 2019
092f739
searching elements: make more modern
iliakan Apr 21, 2019
d67929c
Updated article.md (better sentence flow)
Apr 21, 2019
b38d426
minor
iliakan Apr 21, 2019
b91abf8
minor
iliakan Apr 21, 2019
74e6955
Merge pull request #932 from cmdlhz/cmdlhz-patch-1
iliakan Apr 21, 2019
7aa1e0f
Corrected grammar
alaabouayed Apr 21, 2019
14163bb
Grammar
alaabouayed Apr 21, 2019
3b3363e
Merge pull request #934 from alaabouayed/patch-2
iliakan Apr 22, 2019
585b77d
Merge pull request #933 from alaabouayed/patch-3
iliakan Apr 22, 2019
203b8f9
fixes
iliakan Apr 22, 2019
3b14ed8
minor
iliakan Apr 22, 2019
8ef9fd2
Clarity
alaabouayed Apr 22, 2019
c5ce557
Fix name of JavaScript
lex111 Apr 23, 2019
6f55ba6
Merge pull request #936 from lex111/js-correct
iliakan Apr 23, 2019
10f84e7
6-data-storage/01-cookie: fix typo
lex111 Apr 23, 2019
b9ca311
Merge pull request #935 from alaabouayed/patch-4
iliakan Apr 23, 2019
735880d
Replace `Node.JS` with `Node.js`
lex111 Apr 23, 2019
4c2ab73
Replace `Github` with `GitHub`
lex111 Apr 23, 2019
190f0de
Corrected grammar
Violet-Bora-Lee Apr 25, 2019
743a0dc
Fix typo
h-n-y Apr 25, 2019
340c147
Merge pull request #939 from Violet-Bora-Lee/patch-1
iliakan Apr 25, 2019
8735adc
Merge pull request #940 from h-n-y/h-n-y-patch-1
iliakan Apr 25, 2019
273e47b
cleanup
iliakan Apr 25, 2019
26989b4
merging all conflicts
iliakan Apr 25, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
up
  • Loading branch information
iliakan committed Mar 26, 2019
commit 0873d43d7280c8d406c9562e5117b1d6e37f031e
2 changes: 1 addition & 1 deletion 1-js/13-modules/01-modules-intro/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ You may want skip those for now if you're reading for the first time, or if you

### Module scripts are deferred

Module scripts are *always* deferred, same effect as `defer` attribute (described in the chapter [](info:onload-ondomcontentloaded)), for both external and inline scripts.
Module scripts are *always* deferred, same effect as `defer` attribute (described in the chapter [](info:script-async-defer)), for both external and inline scripts.

In other words:
- external module scripts `<script type="module" src="...">` don't block HTML processing.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Page lifecycle: DOMContentLoaded, load, beforeunload, unload
# Page: DOMContentLoaded, load, beforeunload, unload

The lifecycle of an HTML page has three important events:

Expand All @@ -10,7 +10,8 @@ Each event may be useful:

- `DOMContentLoaded` event -- DOM is ready, so the handler can lookup DOM nodes, initialize the interface.
- `load` event -- additional resources are loaded, we can get image sizes (if not specified in HTML/CSS) etc.
- `beforeunload/unload` event -- the user is leaving: we can check if the user saved the changes and ask them whether they really want to leave.
- `beforeunload` event -- the user is leaving: we can check if the user saved the changes and ask them whether they really want to leave.
- `unload` -- the user almost left, but we still can initiate some operations, such as sending out statistics.

Let's explore the details of these events.

Expand All @@ -22,6 +23,7 @@ We must use `addEventListener` to catch it:

```js
document.addEventListener("DOMContentLoaded", ready);
// not "document.onDOMContentLoaded = ..."
```

For instance:
Expand All @@ -43,40 +45,46 @@ For instance:
<img id="img" src="https://en.js.cx/clipart/train.gif?speed=1&cache=0">
```

In the example the `DOMContentLoaded` handler runs when the document is loaded and does not wait for the image to load. So `alert` shows zero sizes.
In the example the `DOMContentLoaded` handler runs when the document is loaded, so it can see all the elements, including `<img>` below.

At the first sight `DOMContentLoaded` event is very simple. The DOM tree is ready -- here's the event. But there are few peculiarities.
But it doesn't wait for the image to load. So `alert` shows zero sizes.

### DOMContentLoaded and scripts
At the first sight `DOMContentLoaded` event is very simple. The DOM tree is ready -- here's the event. There are few peculiarities though.

When the browser initially loads HTML and comes across a `<script>...</script>` in the text, it can't continue building DOM. It must execute the script right now. So `DOMContentLoaded` may only happen after all such scripts are executed.
### DOMContentLoaded and scripts

External scripts (with `src`) also put DOM building to pause while the script is loading and executing. So `DOMContentLoaded` waits for external scripts as well.
When the browser processes an HTML-document and comes across a `<script>` tag, it needs to execute before continuing building the DOM. That's a precaution, as scripts may want to modify DOM, and even `document.write` into it, so `DOMContentLoaded` has to wait.

The only exception are external scripts with `async` and `defer` attributes. They tell the browser to continue processing without waiting for the scripts. This lets the user see the page before scripts finish loading, which is good for performance.
So DOMContentLoaded definitely happens after such scripts:

### Scripts with `async` and `defer`
```html run
<script>
document.addEventListener("DOMContentLoaded", () => {
console.log("DOM ready!");
});
</script>

Attributes `async` and `defer` work only for external scripts. They are ignored if there's no `src`.
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.3.0/lodash.js"></script>

Both of them tell the browser that it may go on working with the page, and load the script "in background", then run the script when it loads. So the script doesn't block DOM building and page rendering.
<script>
alert("Library loaded, inline script executed");
</script>
```

There are two differences between them.
In the example above, we first see "Library loaded...", and then "DOM ready!" (all scripts are executed).

| | `async` | `defer` |
|---------|---------|---------|
| Order | Scripts with `async` execute *in the load-first order*. Their document order doesn't matter -- which loads first runs first. | Scripts with `defer` always execute *in the document order* (as they go in the document). |
| `DOMContentLoaded` | Scripts with `async` may load and execute while the document has not yet been fully downloaded. That happens if scripts are small or cached, and the document is long enough. | Scripts with `defer` execute after the document is loaded and parsed (they wait if needed), right before `DOMContentLoaded`. |
```warn header="Scripts with `async`, `defer` or `type=\"module\"` don't block DOMContentLoaded"

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.
Script attributes `async` and `defer`, that we'll cover [a bit later](info:script-async-defer), don't block DOMContentLoaded. [Javascript modules](info:modules) behave like `defer`, they don't block it too.

While `defer` is used for scripts that need DOM and/or their relative execution order is important.
So here we're talking about "regular" scripts, like `<script>...</script>`, or `<script src="..."></script>`.
```

### DOMContentLoaded and styles

External style sheets don't affect DOM, and so `DOMContentLoaded` does not wait for them.
External style sheets don't affect DOM, so `DOMContentLoaded` does not wait for them.

But there's a pitfall: if we have a script after the style, then that script must wait for the stylesheet to execute:
But there's a pitfall. Isf we have a script after the style, then that script must wait until the stylesheet loads:

```html
<link type="text/css" rel="stylesheet" href="style.css">
Expand All @@ -98,7 +106,6 @@ For instance, if the page has a form with login and password, and the browser re

So if `DOMContentLoaded` is postponed by long-loading scripts, then autofill also awaits. You probably saw that on some sites (if you use browser autofill) -- the login/password fields don't get autofilled immediately, but there's a delay till the page fully loads. That's actually the delay until the `DOMContentLoaded` event.

One of minor benefits in using `async` and `defer` for external scripts -- they don't block `DOMContentLoaded` and don't delay browser autofill.

## window.onload [#window-onload]

Expand All @@ -123,15 +130,15 @@ The example below correctly shows image sizes, because `window.onload` waits for

When a visitor leaves the page, the `unload` event triggers on `window`. We can do something there that doesn't involve a delay, like closing related popup windows.

This event is a good place to send out analytics.
The notable exception is sending analytics.

E.g. we have a script that gathers some data about mouse clicks, scrolls, viewed page areas -- the statistics that can help us to see what users want.
Let's say we gather data about how the page is used: mouse clicks, scrolls, viewed page areas, and so on.

Then `onunload` is the best time to send it out. Regular networking methods such as [fetch](info:fetch-basics) or [XMLHttpRequest](info:xmlhttprequest) don't work well, because we're in the process of leaving the page.
Naturally, `unload` event is when the user leaves us, and we'd like to save the data on our server.

So, there exist `navigator.sendBeacon(url, data)` method for such needs, described in the specification <https://w3c.github.io/beacon/>.
There exists a special `navigator.sendBeacon(url, data)` method for such needs, described in the specification <https://w3c.github.io/beacon/>.

It sends the data in background. The transition to another page is not delayed: the browser leaves the page and performs `sendBeacon` in background.
It sends the data in background. The transition to another page is not delayed: the browser leaves the page, but still performs `sendBeacon`.

Here's how to use it:
```js
Expand All @@ -148,37 +155,48 @@ window.addEventListener("unload", function() {

When the `sendBeacon` request is finished, the browser probably has already left the document, so there's no way to get server response (which is usually empty for analytics).

There's also a `keepalive` flag for doing such "after-page-left" requests in [fetch](info:fetch-basics) method for generic network requests. You can find more information in the chapter <info:fetch-api>.


If we want to cancel the transition to another page, we can't do it here. But we can use another event -- `onbeforeunload`.

## window.onbeforeunload [#window.onbeforeunload]

If a visitor initiated navigation away from the page or tries to close the window, the `beforeunload` handler asks for additional confirmation.

It may return a string with the question. Historically browsers used to show it, but as of now only some of them do. That's because certain webmasters abused this event handler by showing misleading and hackish messages.
If we cancel the event, the browser may ask the visitor if they are sure.

You can try it by running this code and then reloading the page.
You can try it by running this code and then reloading the page:

```js run
window.onbeforeunload = function() {
return "There are unsaved changes. Leave now?";
return false;
};
```

```online
Or you can click on the button in `<iframe>` below to set the handler, and then click the link:
For historical reasons, returning a non-empty string also counts as canceling the event. Some time ago browsers used show it as a message, but as the [modern specification](https://html.spec.whatwg.org/#unloading-documents) says, they shouldn't.

Here's an example:

[iframe src="window-onbeforeunload" border="1" height="80" link edit]
```js run
window.onbeforeunload = function() {
return "There are unsaved changes. Leave now?";
};
```

The behavior was changed, because some webmasters abused this event handler by showing misleading and annoying messages. So right now old browsers still may show it as a message, but aside of that -- there's no way to customize the message shown to the user.

## readyState

What happens if we set the `DOMContentLoaded` handler after the document is loaded?

Naturally, it never runs.

There are cases when we are not sure whether the document is ready or not, for instance an external script with `async` attribute loads and runs asynchronously. Depending on the network, it may load and execute before the document is complete or after that, we can't be sure. So we should be able to know the current state of the document.
There are cases when we are not sure whether the document is ready or not. We'd like our function to execute when the DOM is loaded, be it now or later.

The `document.readyState` property tells us about the current loading state.

The `document.readyState` property gives us information about it. There are 3 possible values:
There are 3 possible values:

- `"loading"` -- the document is loading.
- `"interactive"` -- the document was fully read.
Expand All @@ -192,13 +210,15 @@ Like this:
function work() { /*...*/ }

if (document.readyState == 'loading') {
// loading yet, wait for the event
document.addEventListener('DOMContentLoaded', work);
} else {
// DOM is ready!
work();
}
```

There's a `readystatechange` event that triggers when the state changes, so we can print all these states like this:
There's also `readystatechange` event that triggers when the state changes, so we can print all these states like this:

```js run
// current state
Expand All @@ -208,15 +228,14 @@ console.log(document.readyState);
document.addEventListener('readystatechange', () => console.log(document.readyState));
```

The `readystatechange` event is an alternative mechanics of tracking the document loading state, it appeared long ago. Nowadays, it is rarely used, but let's cover it for completeness.
The `readystatechange` event is an alternative mechanics of tracking the document loading state, it appeared long ago. Nowadays, it is rarely used.

What is the place of `readystatechange` among other events?
Let's see the full events flow for the completeness.

To see the timing, here's a document with `<iframe>`, `<img>` and handlers that log events:
Here's a document with `<iframe>`, `<img>` and handlers that log events:

```html
<script>
function log(text) { /* output the time and message */ }
log('initial readyState:' + document.readyState);

document.addEventListener('readystatechange', () => log('readyState:' + document.readyState));
Expand Down Expand Up @@ -244,23 +263,22 @@ The typical output:
6. [4] readyState:complete
7. [4] window onload

The numbers in square brackets denote the approximate time of when it happens. The real time is a bit greater, but events labeled with the same digit happen approximately at the same time (+- a few ms).
The numbers in square brackets denote the approximate time of when it happens. Events labeled with the same digit happen approximately at the same time (+- a few ms).

- `document.readyState` becomes `interactive` right before `DOMContentLoaded`. These two events actually mean the same.
- `document.readyState` becomes `interactive` right before `DOMContentLoaded`. These two things actually mean the same.
- `document.readyState` becomes `complete` when all resources (`iframe` and `img`) are loaded. Here we can see that it happens in about the same time as `img.onload` (`img` is the last resource) and `window.onload`. Switching to `complete` state means the same as `window.onload`. The difference is that `window.onload` always works after all other `load` handlers.


## Lifecycle events summary
## Summary

Page lifecycle events:
Page load events:

- `DOMContentLoaded` event triggers on `document` when DOM is ready. We can apply JavaScript to elements at this stage.
- All inline scripts and scripts with `defer` are already executed.
- Async scripts may execute both before and after the event, depends on when they load.
- Script such as `<script>...</script>` or `<script src="..."></script>` block DOMContentLoaded, the browser waits for them to execute.
- Images and other resources may also still continue loading.
- `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.
- `beforeunload` event on `window` triggers when the user wants to leave the page. If it returns a string, the browser shows a question whether the user really wants to leave or not.
- `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.
- `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).
- `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`.
- `document.readyState` is the current state of the document, changes can be tracked in the `readystatechange` event:
- `loading` -- the document is loading.
- `interactive` -- the document is parsed, happens at about the same time as `DOMContentLoaded`, but before it.
Expand Down
File renamed without changes.
Loading