Name

Document — an HTML or XML document

Inherits from

Node

Synopsis

A Document object is a Node that serves as the root of a document tree. The document Element property is the root Element of the document. A Document node may have other children (such as Comment and DocumentType nodes), but it has only one Element child that holds all the content of the document.

You most commonly obtain a Document object via the document property of a Window. Document objects are also available through the contentDocument property of IFrame elements or the ownerDocument property of any Node.

Most of the properties of a Document object provide access to elements of the document or to other important objects associated with the document. A number of Document methods do the same thing: provide a way to look up elements within the document tree. Many other Document methods are “factory methods” that create elements and related objects.

Documents, like the Elements they contain, are event targets. They implement the methods defined by EventTarget, and they also support quite a few event handler properties.

You can create new Document objects by using the createDocument() and createHTML Document() methods of the DOMImplementation:

document.implementation.createHTMLDocument("New Doc");

It is also possible to load an HTML or XML file from the network and parse it into a Document object. See the responseXML property of the XMLHttpRequest object.

The reference entry for HTMLDocument, which appeared in previous versions ...

Get JavaScript: The Definitive Guide, 6th Edition 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.