Chapter 2. Document Nodes

2.1 document Node Overview

The HTMLDocument constructor (which inherits from document) creates a DOCUMENT_NODE (i.e., a window.document) in the DOM. To verify this, I can simply ask which constructor was used in the creation of the document node object.

Live code

<!DOCTYPE html>
<html lang="en">

console.log(window.document.constructor); /* logs function HTMLDocument() { [native code] } */
console.log(window.document.nodeType); /* logs 9, which is a numeric key mapping to DOCUMENT_NODE */


The preceding code concludes that the HTMLDocument constructor function constructs the window.document node object and that this node is a DOCUMENT_NODE object.


Both Document and HTMLDocument constructors are typically instantiated by the browser when you load an HTML document. However, using document.implementation.createHTMLDocument(), it’s possible to create your own HTML document outside the one currently loaded in the browser. In addition to createHTMLDocument(), it’s possible to create a document object that has yet to be set up as an HTML document using createDocument(). An example use of these methods might be to programmatically provide an HTML document to an iframe.

2.2 HTMLDocument Properties and Methods (Including Inherited)

To get accurate information pertaining to the available properties and methods on an HTMLDocument node, it’s best to ignore the specification and to ask the browser what is available. Examine the arrays ...

Get DOM Enlightenment now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.