Skip to content

Commit e5ff949

Browse files
committed
Merge branch 'master' of github.com:javascript-tutorial/en.javascript.info into sync-5e9eca37
2 parents ae17d19 + 5e9eca3 commit e5ff949

7 files changed

Lines changed: 29 additions & 20 deletions

File tree

2-ui/2-events/02-bubbling-and-capturing/article.md

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ Here's the picture of a click on `<td>` inside a table, taken from the specifica
130130

131131
![](eventflow.png)
132132

133-
That is: for a click on `<td>` the event first goes through the ancestors chain down to the element (capturing), then it reaches the target, and then it goes up (bubbles), calling handlers on its way.
133+
That is: for a click on `<td>` the event first goes through the ancestors chain down to the element (capturing phase), then it reaches the target and triggers there (target phase), and then it goes up (bubbling phase), calling handlers on its way.
134134

135135
**Before we only talked about bubbling, because the capturing phase is rarely used. Normally it is invisible to us.**
136136

@@ -149,6 +149,7 @@ There are two possible values of the `capture` option:
149149
- If it's `false` (default), then the handler is set on the bubbling phase.
150150
- If it's `true`, then the handler is set on the capturing phase.
151151

152+
152153
Note that while formally there are 3 phases, the 2nd phase ("target phase": the event reached the element) is not handled separately: handlers on both capturing and bubbling phases trigger at that phase.
153154

154155
Let's see both capturing and bubbling in action:
@@ -179,30 +180,39 @@ The code sets click handlers on *every* element in the document to see which one
179180

180181
If you click on `<p>`, then the sequence is:
181182

182-
1. `HTML` -> `BODY` -> `FORM` -> `DIV` -> `P` (capturing phase, the first listener), and then:
183-
2. `P` -> `DIV` -> `FORM` -> `BODY` -> `HTML` (bubbling phase, the second listener).
184-
185-
Please note that `P` shows up two times: at the end of capturing and at the start of bubbling.
183+
1. `HTML` -> `BODY` -> `FORM` -> `DIV` (capturing phase, the first listener):
184+
2. `P` (target phrase, triggers two times, as we've set two listeners: capturing and bubbling)
185+
3. `DIV` -> `FORM` -> `BODY` -> `HTML` (bubbling phase, the second listener).
186186

187187
There's a property `event.eventPhase` that tells us the number of the phase on which the event was caught. But it's rarely used, because we usually know it in the handler.
188188

189189
```smart header="To remove the handler, `removeEventListener` needs the same phase"
190190
If we `addEventListener(..., true)`, then we should mention the same phase in `removeEventListener(..., true)` to correctly remove the handler.
191191
```
192192
193+
````smart header="Listeners on same element and same phase run in their set order"
194+
If we have multiple event handlers on the same phase, assigned to the same element with `addEventListener`, they run in the same order as they are created:
195+
196+
```js
197+
elem.addEventListener("click", e => alert(1)); // guaranteed to trigger first
198+
elem.addEventListener("click", e => alert(2));
199+
```
200+
````
201+
202+
193203
## Summary
194204
195-
The event handling process:
205+
When an event happens -- the most nested element where it happens gets labeled as the "target element" (`event.target`).
196206
197-
- When an event happens -- the most nested element where it happens gets labeled as the "target element" (`event.target`).
198-
- Then the event first moves from the document root down to the `event.target`, calling handlers assigned with `addEventListener(...., true)` on the way (`true` is a shorthand for `{capture: true}`).
199-
- Then the event moves from `event.target` up to the root, calling handlers assigned using `on<event>` and `addEventListener` without the 3rd argument or with the 3rd argument `false`.
207+
- Then the event moves down from the document root to `event.target`, calling handlers assigned with `addEventListener(...., true)` on the way (`true` is a shorthand for `{capture: true}`).
208+
- Then handlers are called on the target element itself.
209+
- Then the event bubbles up from `event.target` up to the root, calling handlers assigned using `on<event>` and `addEventListener` without the 3rd argument or with the 3rd argument `false/{capture:false}`.
200210
201211
Each handler can access `event` object properties:
202212
203213
- `event.target` -- the deepest element that originated the event.
204214
- `event.currentTarget` (=`this`) -- the current element that handles the event (the one that has the handler on it)
205-
- `event.eventPhase` -- the current phase (capturing=1, bubbling=3).
215+
- `event.eventPhase` -- the current phase (capturing=1, target=2, bubbling=3).
206216
207217
Any event handler can stop the event by calling `event.stopPropagation()`, but that's not recommended, because we can't really be sure we won't need it above, maybe for completely different things.
208218

2-ui/4-forms-controls/3-events-change-input/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ For other elements: `select`, `input type=checkbox/radio` it triggers right afte
2929

3030
## Event: input
3131

32-
The `input` event triggers every time after a value is modified.
32+
The `input` event triggers every time after a value is modified by the user.
3333

3434
Unlike keyboard events, it triggers on any value change, even those that does not involve keyboard actions: pasting with a mouse or using speech recognition to dictate the text.
3535

2-ui/5-loading/01-onload-ondomcontentloaded/article.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,12 @@ window.addEventListener("unload", function() {
150150
```
151151
152152
- The request is sent as POST.
153-
- We can send not only a string, but also forms and other formats, as described in the chapter <info:fetch-basics>, but usually it's a stringified object.
153+
- We can send not only a string, but also forms and other formats, as described in the chapter <info:fetch>, but usually it's a stringified object.
154154
- The data is limited by 64kb.
155155
156156
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).
157157
158-
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>.
158+
There's also a `keepalive` flag for doing such "after-page-left" requests in [fetch](info:fetch) method for generic network requests. You can find more information in the chapter <info:fetch-api>.
159159
160160
161161
If we want to cancel the transition to another page, we can't do it here. But we can use another event -- `onbeforeunload`.

2-ui/99-ui-misc/01-mutation-observer/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ let hello = "world";
130130

131131
Everything's simple so far, right? There are `<pre>` code snippets in HTML, we highlight them.
132132

133-
Now let's go on. Let's say we're going to dynamically fetch materials from a server. We'll study methods for that [later in the tutorial](info:fetch-basics). For now it only matters that we fetch an HTML article from a webserver and display it on demand:
133+
Now let's go on. Let's say we're going to dynamically fetch materials from a server. We'll study methods for that [later in the tutorial](info:fetch). For now it only matters that we fetch an HTML article from a webserver and display it on demand:
134134

135135
```js
136136
let article = /* fetch new content from server */

3-frames-and-windows/03-cross-window-communication/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ An `<iframe>` tag hosts a separate embedded window, with its own separate `docum
3333
We can access them using properties:
3434

3535
- `iframe.contentWindow` to get the window inside the `<iframe>`.
36-
- `iframe.contentDocument` to get the document inside the `<iframe>`, короткий аналог `iframe.contentWindoe.document`.
36+
- `iframe.contentDocument` to get the document inside the `<iframe>`, короткий аналог `iframe.contentWindow.document`.
3737

3838
When we access something inside the embedded window, the browser checks if the iframe has the same origin. If that's not so then the access is denied (writing to `location` is an exception, it's still permitted).
3939

4-binary/03-blob/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ While `ArrayBuffer`, `Uint8Array` and other `BufferSource` are "binary data", a
233233

234234
That makes Blobs convenient for upload/download operations, that are so common in the browser.
235235

236-
Methods that perform web-requests, such as [XMLHttpRequest](info:xmlhttprequest), [fetch](info:fetch-basics) and so on, can work with `Blob` natively, as well as with other binary types.
236+
Methods that perform web-requests, such as [XMLHttpRequest](info:xmlhttprequest), [fetch](info:fetch) and so on, can work with `Blob` natively, as well as with other binary types.
237237

238238
We can easily convert betweeen `Blob` and low-level binary data types:
239239

9-regular-expressions/13-regexp-multiline-mode/article.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ alert( str.match(/^\d+/gm) ); // 1, 2, 33
2020
*/!*
2121
```
2222

23-
Without the flag `pattern:/.../m` only the first number is matched:
23+
The regexp engine moves along the text and looks for a line start `pattern:^`, when finds -- continues to match the rest of the pattern `pattern:\d+`.
2424

25+
Without the flag `pattern:/.../m` only the first number is matched:
2526

2627
```js run
2728
let str = `1st place: Winnie
@@ -33,9 +34,7 @@ alert( str.match(/^\d+/g) ); // 1
3334
*/!*
3435
```
3536

36-
That's because by default a caret `pattern:^` only matches at the beginning of the text, and in the multiline mode -- at the start of a line.
37-
38-
The regular expression engine moves along the text and looks for a string start `pattern:^`, when finds -- continues to match the rest of the pattern `pattern:\d+`.
37+
That's because by default a caret `pattern:^` only matches at the beginning of the text, and in the multiline mode -- at the start of any line.
3938

4039
## Line end $
4140

0 commit comments

Comments
 (0)