Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML DOM Document Object

HTML DOM Document Object


HTML DOM Nodes

In the HTML DOM (Document Object Model), every element is a node. Node:


Document Object

When a browser loads an HTML document, it becomes a Document object. Document Object.

The Document object is the root node of an HTML document.

The Document object allows us to access all elements in an HTML page from a script.

**Tip:** The Document object is part of the Window object and can be accessed through the window.document property.


Browser support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the Document object.


Document Object Properties and Methods

The following properties and methods can be used in an HTML document:

Attribute / Method Description
document.activeElement Returns the currently focused element.
document.addEventListener() Adds a handler to the document.
document.adoptNode(node) Returns an adapted node from another document to the current document.
document.anchors Returns references to all Anchor objects in the document.
document.applets

Returns references to all Applet objects in the document.

Note: No longer supported in HTML5. .

document.baseURI Returns the absolute base URI of the document.
document.body Returns the body element of the document.
document.close() Closes the output stream opened with the document.open() method and displays the selected data.
document.cookie Sets or returns all cookies associated with the current document.
document.createAttribute() Creates an attribute node.
document.createComment() The createComment() method creates a comment node.
document.createDocumentFragment() Creates an empty DocumentFragment object and returns it.
document.createElement() Create an element node.
document.createTextNode() Create a text node.
document.doctype Returns the document type declaration (DTD) associated with the document.
document.documentElement Returns the root node of the document.
document.documentMode Returns the mode used by the browser to render the document.
document.documentURI Sets or returns the location of the document.
document.domain Returns the domain name of the current document.
document.domConfig Deprecated.Returns the configuration used when normalizeDocument() is called.
document.embeds Returns a collection of all embedded content (embed) in the document.
document.forms Returns references to all Form objects in the document.
document.getElementsByClassName() Returns a collection of all elements with the specified class name in the document, as a NodeList object.
document.getElementById() Returns a reference to the first object with the specified id.
document.getElementsByName() Returns a collection of objects with the specified name.
document.getElementsByTagName() Returns a collection of objects with the specified tag name.
document.images Returns references to all Image objects in the document.
document.implementation Returns the DOMImplementation object that handles the document.
document.importNode() Copies a node from another document to this document for use.
document.inputEncoding Returns the encoding used for the document (at parsing time).
document.lastModified Returns the date and time when the document was last modified.
document.links Returns references to all Area and Link objects in the document.
document.normalize() Removes empty text nodes and joins adjacent nodes.
document.normalizeDocument() Removes empty text nodes and joins adjacent nodes of the
document.open() Opens a stream to collect output from any document.write() or document.writeln() method.
document.querySelector() Returns the first element in the document that matches the specified CSS selector.
document.querySelectorAll() document.querySelectorAll() is a new method introduced in HTML5 that returns a list of all element nodes in the document matching a CSS selector.
document.readyState Returns the document status (loading...).
document.referrer Returns the URL of the document that loaded the current document.
document.removeEventListener() Removes an event handler from the document (added by the addEventListener() method).
document.renameNode() Renames an element or attribute node.
document.scripts Returns a collection of all scripts in the page.
document.strictErrorChecking Sets or returns whether error checking is enforced.
document.title Returns the title of the current document.
document.URL Returns the complete URL of the document.
document.write() Writes HTML expressions or JavaScript code to the document.
document.writeln() Equivalent to the write() method, except that it writes a newline after each expression.

Warning !!!

In the W3C DOM Core, the document object inherits all properties and methods of the node object.

Many properties and methods are meaningless for a document.

An HTML document object can avoid using these node objects and properties:

 Attribute / Method Reasons to Avoid
document.attributes The document has no such attribute.
document.hasAttributes() The document has no such attribute.
document.nextSibling The document has no next node.
document.nodeName This is usually #document.
document.nodeType This is usually 9 (DOCUMENT_NODE).
document.nodeValue The document has no node value.
document.ownerDocument The document has no owner document.
document.ownerElement The document has no own node.
document.parentNode The document has no parent node.
document.previousSibling The document has no sibling node.
document.textContent The document has no text node.