How it works
Each EventTarget has a get the parent algorithm, and dispatch calls it repeatedly to build the path. A node returns its assigned slot if it has one, otherwise its parent. A Document returns its Window, except for load events or when it has no browsing context. A shadow root returns its host unless the event isn't composed and the shadow root is the root of the original target. A Window, and a plain new EventTarget(), return null, which is why a standalone EventTarget never propagates anywhere.
With the path built, dispatch invokes each entry twice: once in reverse order with phase "capturing", once in order with phase "bubbling". At each entry, inner invoke takes a clone of that node's listener list before calling anything. So a listener added to the current node during dispatch doesn't run for this event, while one added to a node the walk hasn't reached yet does. A removed listener is skipped even if it's in the clone, because removal sets its removed flag.
// <div id="outer"><button id="btn">Go</button></div><div id="other"></div>
const [outer, btn, other] = ['outer', 'btn', 'other'].map((id) => document.getElementById(id));
const log = [];
btn.addEventListener('click', () => {
log.push('btn');
btn.addEventListener('click', () => log.push('btn, added late'));
outer.addEventListener('click', () => log.push('outer, added late'));
other.append(btn); // move the target mid-dispatch
});
outer.addEventListener('click', (event) => {
log.push(`outer, target now in #${event.target.parentNode.id}`);
});
btn.click();
console.log(log.join(' | '));
// → btn | outer, target now in #other | outer, added late
#outer still runs although the button has left it, because the path was built before the first listener.
If a listener throws, the error is reported to window's error event and dispatch moves on to the next listener; one broken handler doesn't stop the others.
Edge cases
A node outside the document has a shorter path: its ancestors only, with no Document or Window.
const div = document.createElement('div');
const span = div.appendChild(document.createElement('span'));
span.addEventListener('ping', (e) => console.log(e.composedPath().map((n) => n.nodeName).join()));
span.dispatchEvent(new Event('ping'));
// → SPAN,DIV
Resource load events stop at the Document, so a capture listener on document is the way to observe every image load in one place:
const img = new Image();
const isImg = (event) => event.target === img;
window.addEventListener('load', (e) => isImg(e) && console.log('window capture'), true);
document.addEventListener('load', (e) => isImg(e) && console.log('document capture'), true);
img.addEventListener('load', () => console.log('img'));
img.src = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
document.body.append(img);
// → document capture
// → img
Chrome 156 and Firefox 159 printed the same for all three blocks.
focus and blur don't bubble, but focusin and focusout do and fire for the same focus changes. Delegated focus handling uses those, or a capture listener.
Real-world scenario
A "click outside to close" handler on document in the bubble phase silently stops working when some component calls stopPropagation() on its clicks. Registering it with capture: true lets it see the click on the way down, before any target-side handler can stop it. The same applies to analytics and focus-trap code that must see every event: capture at the top, and don't call stopPropagation() there.
Trade-offs
Capture listeners on document run before every component's own handlers, which is what you want for observation and wrong for behaviour that a component should be able to override. Bubble-phase handlers let inner code run first and decide (preventDefault()), at the cost of being skippable by stopPropagation(). Choose by asking who should get the last word.