DOM¶
A deep dive into the Document Object Model.
Introduction¶
Document Object Model (often abbreviated as DOM) is the tree data structured resulted from parsing HTML. It consists of one or more instances of subclasses of Node and represents the document tree structure. Parsing a simple HTML like this:
<!DOCTYPE html>
<html>
<body>hi</body>
</html>
Will generate the following six distinct DOM nodes:
- Document
- DocumentType
- HTMLHtmlElement
- HTMLHeadElement
- HTMLBodyElement
- Text with the value of “hi”
Note that HTMLHeadElement (i.e. <head>
) is created implicitly by WebKit
per the way HTML parser is specified.
Broadly speaking, DOM node divides into the following categories:
- Container nodes such as Document, Element, and DocumentFragment.
- Leaf nodes such as DocumentType, Text, and Attr.
Document node, as the name suggests a single HTML, SVG, MathML, or other XML document, and is the owner of every node in the document. It is the very first node in any document that gets created and the very last node to be destroyed.
Note that a single web page may consist of multiple documents since iframe and object elements may contain a child frame, and form a frame tree. Because JavaScript can open a new window under user gestures and have access back to its opener, multiple web pages across multiple tabs might be able to communicate with one another via JavaScript API such as postMessage.