- DOM (Document Object Model) is a tree-like representation of your web page.
- It converts your HTML and XML documents into objects and nodes so JavaScript can read, modify, or delete elements dynamically.
- Think of it as a live interface between your code and what you see in the browser.
When
- The browser creates the DOM right after it downloads and parses the HTML file, before rendering the page fully.
- It’s updated in real time when JavaScript manipulates elements (e.g., adding or removing nodes).
Where
- The DOM exists inside the browser’s memory as part of its rendering engine (e.g., Blink for Chrome, Gecko for Firefox).
- You interact with it through JavaScript in the browser environment (
window.document
).
How
- Parse HTML → The browser reads raw HTML and tokenizes it.
- Create Nodes → Each tag, attribute, and piece of text becomes a node.
- Build Tree → Nodes are connected into a tree structure (the DOM tree).
- Attach CSSOM → CSS is parsed into a CSS Object Model (CSSOM) and combined with the DOM for rendering.
- Render → The browser paints the pixels on screen based on the DOM + CSSOM.
Example:
If you write:
<body>
<h1>Hello</h1>
<p>World</p>
</body>
The DOM tree looks like:
Document
└─ html
└─ body
├─ h1 ("Hello")
└─ p ("World")
You can access the <h1>
using JavaScript:
Top comments (0)