Your Ultimate Guide to Understanding DOM Events
Contents
-
Introduction to the DOM Events Course
-
High-Level DOM Events Concepts
-
-
- Animation
- Asynchronous data fetching
- Clipboard
- Composition
- CSS transition
- Database
- DOM mutation
- Drag'n'drop, Wheel
- Focus
- Form
- Fullscreen
- Gamepad
- Gestures
- History
- HTML element content display management
- Inputs
- Keyboard
- Loading/unloading documents
- Manifests
- Media
- Messaging
- Mouse
- Network/Connection
- Payments
- Performance
- Pointer
- Promise rejection
- Sockets
- SVG
- Text selection
- Touch
- Virtual reality
- RTC (real time communication)
- Server-sent events
- Speech
- Workers
-
-
- 4 Event Targets
- Listen to Events using HTML Attribute Event Handlers
-
HTML Attribute Event Handler
- GlobalEventHandlers
- Bubble phase only!
- DOM0 Event Handler
- Event Handler Content Attribute
- Listen to Events using Object Property Event Handlers
-
Object Property Event Handler
- Bubble phase only!
- DOM0 Event Handler
- Event Handler IDL Attribute
-
Understand the Relationship Between HTML Attribute and Object Property Event Handlers
-
Add an Event Listener with addEventListener
-
addEventListener
-
DOM2 / DOM2+ Event Listener
-
Bubble phase by default
-
Syntax
addEventListener(type, listener) addEventListener(type, listener, options) addEventListener(type, listener, useCapture)
- Remove an Event Listener with removeEventListener
-
removeEventListener
-
Choose an Event Listener Mechanism
-
The Execution Order of Event Listeners
-
The Execution Order of Event Listeners in the Target Phase
-
The Event Object
- Clicking the parent element and the button shall get different results.
- Event
- Event.timeStamp
- Event.returnValue #deprecated
- Event.currentTarget
- The
currentTarget
property changes as the event flows through the different event listeners. And it's also set to null when the event is finished dispatching.
- The
- Log Events to the Console
- Developer Tools
- Console
This value was evaluated upon first expanding.
- Console
- Cancel Events
- Event.preventDefault()
- Event.defaultPrevented #readonly
- mousedown
- Default action
Varies: Start a drag/drop operation; start a text selection; start a scroll/pan interaction (in combination with the middle mouse button, if supported)
- Default action
- Cancel Bespoke Events
-
error
-
beforeunload
- Stop Events
- The Event Delegation Pattern
- Create and Dispatch Synthetic Events
- synthetic
- / sɪnˈθetɪk /
- Synthetic products are made from chemicals or artificial substances rather than from natural ones.
- e.g. Boots made from synthetic materials can usually be washed in a machine.
- Event()
- Event.timeStamp
- The
event.tiemStamp
property is set when the event is created and not when it's dispatched.
- The
- Event.timeStamp
- MouseEvent()
- EventTarget.dispatchEvent()
- When an event is dispatched, its internal
stopPropagation
andstopImmediatePropagation
flags are cleared, but the internalcanceled
flag is not cleared.
- When an event is dispatched, its internal
- CustomEvent()
- Deprecated Event Creation Mechanisms
- Document.createEvent() #deprecated
- Events are Dispatched Synchronously
- EventTarget.dispatchEvent()
-
Unlike "native" events, which are fired by the browser and invoke event handlers asynchronously via the event loop,
dispatchEvent()
invokes event handlers synchronously. All applicable event handlers are called and return beforedispatchEvent()
returns.
-
-
Add and Remove Event Listeners while an Event is Dispatching
-
DOM Events and the Event Loop
- The event loop
- Event loops
-
An event loop has one or more task queues. A task queue is a set of tasks.
-
- DOM Events and Microtasks
-
When a task for an event is being executed on the Call Stack, all event listeners for the event are executed.
-
When the Call Stack is empty, our microtask queue will be executed. What this means is that microtasks queued within an event listener will be executed immediately after that event listener, and before the next event listener is executed.
- Improve Scroll Performance with Passive Event Listeners
-
Problem
-
If a user is trying to scroll the page, the browser has to wait for our
wheel
event listeners to be processed before the browser can know if native scrolling is allowed to happen. It has to wait to see if the event was canceled. This delay can be significant and can make a web page feel unresponsive. -
This delay can be even worse if the browser is already executing some other tasks in the Call Stack, which needs to be finished before the
wheel
event can even start to be processed. -
The solution to this problem is passive event listeners.
-
If all of our event listeners on our event path are passive, the browser does not need to wait for the event path to be processed to know if an event will be canceled. It already knows the event cannot be canceled. The browser can allow the native scroll to start straight away without needing to wait for any JavaScript.
-
-
Passive event listeners
- EventTarget.addEventListener()
- Parameters
- options
- passive
-
A
boolean
value that, iftrue
, indicates that the function specified by listener will never callpreventDefault()
.
-
- passive
- options
- Parameters
- Use passive listeners to improve scrolling performance
- EventTarget.addEventListener()
-
Scrolling
-
Default Passive Values on the Body Element
-
Synchronous and Asynchronous Events (Ordered and Unordered Events)
- Synchronous and asynchronous events
-
EXAMPLE 3
-
During loading of a document, an inline script element is parsed and executed. The
load
event is queued to be fired asynchronously at the script element. However, because it is an async event, its order with relation to other synchronous events fired during document load (such as theDOMContentLoaded
event from [HTML5]) is not guaranteed.
-
-
focus
- Window Reflecting Body Element Event Handlers
- Window-reflecting body element event handler set
- onblur
- onerror
- onfocus
- onload
- onresize
- onscroll
- Debug and Inspect Event Listeners with Chrome Developer Tools
- Chrome DevTools
- Console
- Console API reference
- Console Utilities API reference
- $0
- getEventListeners(object)
- monitorEvents(object [, events])
- unmonitorEvents(object [, events])
- Sources
- Console
- Debug Event Listener Performance with Chrome Developer Tools
- Chrome DevTools
- Performance
Known Issues
- My document and code are different from the official version.