DOM, Events and Browser APIs
How the page is built, how events travel, what makes rendering slow, and the browser security rules that shape every front-end design.
Key points
- 1
Events capture down from window to the target, then bubble back up.
targetis where the event started;currentTargetis where the listener sits. Delegate withe.target.closest(selector). - 2
stopPropagationstops other elements,stopImmediatePropagationalso stops other listeners on the same element, andpreventDefaultcancels the default action. They are independent. - 3
Reading geometry (offsetWidth, getBoundingClientRect, scrollTop) after a style write forces a synchronous layout. Batch reads before writes, and animate only transform and opacity.
- 4
deferand module scripts run in document order after parsing and before DOMContentLoaded;asyncscripts run as soon as they arrive. load waits for images. - 5
fetch rejects only on network errors (check
response.ok), a body can be read once, credentials default to same-origin, and there is no default timeout. - 6
CORS limits what a page can read; it does not stop requests being sent, so it is not a CSRF defence. Use SameSite cookies, tokens and Origin checks.
- 7
Keep secrets out of reach of script: HttpOnly cookies, textContent instead of innerHTML, exact origin checks on postMessage, and a strict CSP.
Common traps
Live collections from
getElementsBy*shrink as you change them inside a loop;querySelectorAllreturns a static list.focus,blur,mouseenterandmouseleavedo not bubble. Use focusin/focusout or a capture-phase listener for delegation.A
storageevent never fires in the tab that made the change, andpushStatenever firespopstate.
Read the source
Test yourself on DOM, Events and Browser APIs
Ten questions, with the answer and explanation after each one.