The Web FrontendDeeper

Kayavak / The Web Frontend / FE·17 DOM & Browser APIs / Deeper

Deeper · The Web Frontend

DOM & Browser APIs

The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.

Why does this exist?

When scripts arrived in 1995–96, each browser exposed the page to them differently: Netscape's document.layers, Microsoft's document.all. Code had to be written twice, and much of it sniffed the browser to decide which half to run. The W3C's Document Object Model (Level 1, 1998) defined one tree of nodes and one API for every browser, but real compatibility took another decade. jQuery (John Resig, 2006) became the most widely used JavaScript library by papering over the differences with one simple API.

As browsers converged, the platform absorbed what libraries had provided: querySelector (selectors from CSS, 2008–09 in browsers), classList, fetch (2015), and observers (MutationObserver, IntersectionObserver, ResizeObserver) that replaced polling and fragile event tricks. Today the DOM is consistent enough that most pages need no compatibility layer at all.

The mental model

Try it yourself first: write your own model in the Mental Model Explorer, then compare.

The DOM is a live tree that the browser renders from. Changing it is cheap; making the browser recalculate layout because you read geometry right after writing is what's expensive.

Question Answer
What is it? The page as a tree of node objects with an API to read, change and listen to it
Problem it solves Letting scripts change what's on screen and react to people, in a standard way
Parts Nodes and elements, events (capture, target, bubble), observers, plus the wider browser APIs (fetch, storage, workers)
How it works Your changes mark parts of the tree dirty; the browser recomputes style and layout lazily, before the next frame or when you ask for geometry
Assumptions You batch writes and reads; event handlers are short
When they change Interleaving writes and geometry reads in a loop forces a layout each time (layout thrashing)
Where it fails Setting innerHTML from untrusted data runs as markup: the classic XSS sink (ch28)
Related ideas Rendering pipeline (ch18); frameworks automate DOM updates (ch21, ch22)

What if?

What if every DOM change re-rendered the page immediately?

Reason it through, then open

Adding a hundred list items would trigger a hundred layouts and paints, and the page would crawl. Browsers instead record that the tree changed and do the work once, before the next frame. The danger case is when you force the work early by asking for a size or position after a change: the browser must lay out now to answer correctly.

What if events didn't bubble?

Reason it through, then open

You'd need a listener on every clickable element, including ones added later. Event delegation (one listener on a container, using event.target.closest() to find what was clicked) would be impossible, and so would the patterns frameworks build on. Bubbling lets one ancestor handle events for a whole, changing subtree.

Where the boundary is

Aspect textContent innerHTML
Treats the string as Text Markup to parse
<img onerror=…> in the data Shows the characters Runs the attacker's code
Speed Fast, no parsing Parses HTML
Use for Anything that came from a user or an API Trusted or sanitised HTML only

How sure are we?

Claim How it's known Where from
W3C DOM Level 1 in 1998 Historical W3C Recommendation
Reading layout geometry after a write forces a synchronous layout Measured The chapter's lab; documented for all engines
Event order is capture, target, then bubble Convention DOM Standard
jQuery was the most used JavaScript library for years Measured Web surveys such as W3Techs; still on many sites

Still open

How to let pages handle untrusted HTML safely without a library: the Sanitizer API (setHTML()) and Trusted Types are shipping unevenly across browsers. Whether frameworks will keep their own virtual DOMs or move to fine-grained updates (signals) is also unsettled (ch21).

What would change your mind? If browsers made layout reads free (always up to date with no recalculation), the "batch reads and writes" advice would disappear. Layout is too expensive for that.

Recall

What causes layout thrashing?

Alternating DOM writes with reads of layout geometry, forcing a synchronous layout each time.

What is event delegation, and what makes it possible?

One listener on an ancestor handling events from many descendants; event bubbling.

Why use `textContent` rather than `innerHTML` for user data?

innerHTML parses the string as markup, so injected HTML can run code.

Why did jQuery become so popular?

It hid the differences between browsers' DOM APIs behind one simple API.

Reflect

Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.