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">
<body>
<script>

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 */

</script>
</body>
</html>

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

Note

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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.