CKTIS / JavaScript
Browser events and propagation
Events let code respond to user actions and changes in page state. Learn capture, target and bubbling, distinguish canceling default behavior from stopping propagation, and understand lifecycle, visibility, scrolling, resizing and input events.
Version: Web APIs defined by web standards, not ECMAScript editions. Check support in target browsers; Node 24 in the repository does not establish browser API support.
Syntax
target.addEventListener(type, handler, options);
target.removeEventListener(type, handler, options);
target.dispatchEvent(event);Model and scope
Browser events are part of the host environment’s APIs, not ECMAScript syntax. The DOM Standard defines the event model, dispatch, propagation, and listener options. The HTML Standard defines when events such as DOMContentLoaded, load, and form control input and change events fire. Do not assign these APIs an ECMAScript edition; Node 24 does not establish browser API support. Compatibility must be checked for target browsers.
Propagation and cancellation
addEventListener(type, handler, options) registers a handler. An event passes through ancestor capture, the target and — when bubbles is true — ancestor bubbling. target identifies the target; currentTarget identifies the object whose listener is running. stopPropagation() stops further propagation without skipping the remaining listeners in the current invocation. However, calling it in a capture listener at the target prevents the separate invocation of non-capture listeners on that same target. stopImmediatePropagation() also skips the remaining listeners in the current invocation. preventDefault() cancels default behavior only for a cancelable event outside a passive listener. It does not stop propagation.
Lifecycle and visibility
DOMContentLoaded on document means parsing and execution of scripts that defer this event have finished, not that every resource has loaded. load on window also waits for load-delaying resources. pageshow and pagehide cover transitions involving the back/forward cache; inspect persisted. On visibilitychange on document, read document.visibilityState. Hidden does not mean closed; do not rely on unload for guaranteed final saving.
Scrolling, resizing and input
scroll concerns the scrolling target, while resize on window concerns viewport resizing; do not assume every event bubbles. Keep frequent handlers inexpensive. input signals user editing, while change signals a control-dependent commitment of a change; assigning .value does not itself dispatch either. Distinguish keydown, pointer events and semantic click; do not make interaction mouse-only.
Listener management
once removes the listener before its callback runs for the first time. If that callback synchronously dispatches another event of the same type, the removed listener does not handle that event. passive rules out effective preventDefault(), and signal enables removal through abortion. For removeEventListener, retain the event type, function reference and capture value. The example uses synchronous dispatchEvent(); its false result means cancellation, not stopped propagation.
Reference identifiers covered by this entry: Page lifecycle, visibility, scroll/resize and input events.
Examples
(() => {
const parent = document.createElement('div');
const child = document.createElement('button');
parent.append(child);
const log = [];
parent.addEventListener('demo', () => log.push('capture'), { capture: true });
child.addEventListener('demo', event => {
log.push('target');
event.preventDefault();
});
parent.addEventListener('demo', () => log.push('bubble'));
const accepted = child.dispatchEvent(new Event('demo', {
bubbles: true,
cancelable: true
}));
console.log(log.join(' > '));
console.log(accepted);
})();Result
capture > target > bubble false
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.
Sources
- DOM Standard — Events, dispatch, propagation, and listener options
- Official reference — developer.mozilla.org: API / PageTransitionEvent
- HTML Standard — End of parsing, DOMContentLoaded, and load
- MDN Web Docs — Document: visibilitychange event
- MDN Web Docs — Window: unload event
- CSSOM View Module — Scroll and resize event processing
- HTML Standard — Common input element events
- UI Events — Keyboard and click events
- Pointer Events