المعرفة:: JavaScript الحالة::مؤرشفة المراجع:: JavaScript Essential Training, The Complete JavaScript Course 2022 From Zero to Expert, https://javascript.info/bubbling-and-capturing, https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#event_bubbling_and_capture
Events
- An Event is a signal generated by a certain DOM node.
- For example, if the user selects a button on a web page, you might want to react to that action by displaying an information box.
- More info on MDN.
Handling Events
addEventListener
- Events can be handled using an Event Listener. There are many events such as
click
,keydown
,submit
,mouseenter
,mouseexit
,mouseleave
, etc.
On-event Properties
- Another way to attach event listeners to elements is to use on-event element’s properties such as
onmouseenter
andonclick
.
- This way isn’t recommended, because
addEventListener
allows us to attach more than event listener to the same element, and usingaddEventListener
we can remove the event handler in case we don’t need it anymore.
HTML Attributes
- This way shouldn’t be used.
Removing an Event Listener
removeEventListener()
can be used to remove an event listener previously registered withaddEventListener
.
Event Bubbling and Capturing (Propagation)
- There are 3 phases of event propagation:
- Capturing phase – the event goes down to the element.
- Target phase – the event reached the target element.
- Bubbling phase – the event bubbles up from the element. (Event handlers pick up events during bubbling phase by default)
Each handler can access event object properties:
event.target
– the deepest element that originated the event.event.currentTarget
(=this
) – the current element that handles the event (the one that has the handler on it).event.eventPhase
– the current phase (capturing=1, target=2, bubbling=3).
Capturing
- The event moves down from the document root to
event.target
, calling handlers assigned withaddEventListener(..., true)
on the way (true is a shorthand for{capture: true}
).
In the capturing phase:
- The browser checks to see if the element’s outer-most ancestor (
<html>
) has aclick
event handler registered on it for the capturing phase, and runs it if so. - Then it moves on to the next element inside
<html>
and does the same thing, then the next one, and so on until it reaches the direct parent of the element that was actually clicked.
Target
- When an event happens – the most nested element where it happens gets labeled as the “target element” (
event.target
).
In the target phase:
- The browser checks to see if the target property has an event handler for the click event registered on it, and runs it if so.
- Then, if bubbles is
true
, it propagates the event to the direct parent of the clicked element, then the next one, and so on until it reaches the<html>
element. Otherwise, if bubbles isfalse
, it doesn’t propagate the event to any ancestors of the target.
Bubbling
- When an event happens on an element, it first runs the handlers on it, then on its parent, then all the way up on other ancestors.
- Almost all events bubble, but not all. For example, a
focus
event does not bubble.
In the bubbling phase, the exact opposite of the capturing phase occurs:
- The browser checks to see if the direct parent of the clicked element has a
click
event handler registered on it for the bubbling phase, and runs it if so. - Then it moves on to the next immediate ancestor element and does the same thing, then the next one, and so on until it reaches the
<html>
element.
Stopping bubbling
event.stopPropagation()
prevents further propagation of the current event in the capturing and bubbling phases.
-
If an element has multiple event handlers on a single event, then even if one of them stops the bubbling, the other ones still execute. To stop the bubbling and prevent handlers on the current element from running, there’s a method
event.stopImmediatePropagation()
. After it no other handlers execute. -
Don’t stop bubbling without a need! Sometimes
event.stopPropagation()
creates hidden pitfalls that later may become problems. There’s usually no real need to prevent the bubbling. A task that seemingly requires that may be solved by other means.
Event Delegation
- Capturing and bubbling allow us to implement one of the most powerful event handling patterns called event delegation.
- The idea is that if we have a lot of elements handled in a similar way, then instead of assigning a handler to each of them – we put a single handler on their common ancestor.
- In the handler we get event.target to see where the event actually happened and handle it.
Passing arguments through event listeners
- We can workaround the problem of not being able to pass arguments into a callback function by using the callback function as a function wrapper to call other functions.
- A better approach is to use bind method to pass the argument to a function copy.
Scroll Event
- The scroll event fires when the document view has been scrolled.
- Scroll events can fired at a high rate, the event handler shouldn’t execute computationally expensive operations such as DOM modifications.
LifeCycle DOM Events
- There are different events that occur in the DOM during a webpage’s life cycle.
- Life cycle means from the moment that the page is first accessed, until the user leaves it.
DOMContentLoaded
- It’s fired by the document as soon as the HTML is completely parsed.
- Happens when HTML has been downloaded and been converted to the DOM tree and all scripts are downloaded and executed.
- It does not wait for images and other external resources to load.
- By placing the script tag at the end of the HTML, we do not need to listen for the DOM content loaded event and then execute our JavaScript code.
load
- The load event is fired by the window as soon as not only the HTML is parsed, but also all the images and external resources like CSS files are also loaded.
beforeunload
- This event is created immediately before a user is about to leave a page.
- It can be used to ask the user if he is sure about leaving the website like the following code: