HTML DOM Document Object

Document Object

When an HTML document is loaded into a web browser, it becomes aDocument Object.

Document Objectis the root node of an HTML document.

Document ObjectisWindow Objectproperties.

Accessed by the following waysDocument Object:

window.document or simply document

Example

let url = window.document.URL;

Try it yourself

let url = document.URL;

Try it yourself

Document Object Properties and Methods

The following properties and methods can be used for HTML documents:

Property / Method Description
all[] Returns a reference to all HTML elements in the document.
activeElement Returns the element that is currently focused in the document.
addEventListener() Attaches an event handler to the document.
adoptNode() Adopts a node from another document.
anchors Deprecated.
applets Deprecated.
baseURI Returns the absolute base URI of the document.
body Sets or returns the body (the <body> element) of the document.
charset Deprecated.
characterSet Returns the character encoding of the document.
close() Closes the output stream previously opened with document.open().
cookie Returns the name/value pairs of all cookies in the document.
createAttribute() Creates an attribute node.
createComment() Creates a Comment node with the specified text.
createDocumentFragment() Creates an empty DocumentFragment node.
createElement() Creates an element node.
createEvent() Creates a new event.
createTextNode() Creates a text node.
defaultView Returns the window object associated with the document, or null if none is available.
designMode Controls whether the entire document should be editable.
doctype Returns the document type declaration associated with the document.
documentElement Returns the Document element of the document (<html> element).
documentMode Deprecated.
documentURI Sets or returns the location of the document.
domain Returns the domain name of the server that loaded the document.
domConfig Deprecated.
embeds Returns the collection of all <embed> elements in the document.
execCommand() Deprecated.
forms Returns the collection of all <form> elements in the document.
getElementById() Returns the element with the specified ID attribute value.
getElementsByClassName() Returns a NodeList of all elements that have the specified class name. HTMLCollection.
getElementsByName() Returns a NodeList of active elements that have the specified name. NodeList.
getElementsByTagName() Returns a NodeList of all elements that have the specified tag name. HTMLCollection.
hasFocus() Returns a boolean value indicating whether the document has focus.
head Returns the <head> element of the document.
images Returns the collection of all <img> elements in the document.
implementation Returns the DOMImplementation object that handles this document.
importNode() Imports a node from another document.
inputEncoding Deprecated.
lastModified Returns the date and time of the last modification of the document.
links Returns a collection of all <a> and <area> elements within the document that have the href attribute.
normalize() Deletes empty text nodes and connects adjacent nodes.
normalizeDocument() Deprecated.
open() Opens the HTML output stream to collect output from document.write().
querySelector() Returns the first element that matches the specified CSS selector in the document.
querySelectorAll() Returns a static NodeList containing all elements that match the specified CSS selector in the document.
readyState Returns the (loading) state of the document.
referrer Returns the URL of the document that loaded the current document.
removeEventListener() Removes an event listener from the document (already used addEventListener() method Additional).
renameNode() Deprecated.
scripts Returns a collection of <script> elements within the document.
strictErrorChecking Deprecated.
title Sets or returns the title of the document.
URL Returns the complete URL of the HTML document.
write() Writes HTML expressions or JavaScript code into the document.
writeln() Similar to write(), but adds a newline character after each statement.

Description of Document object

The HTMLDocument interface extends the DOM Document interface, defining HTML-specific properties and methods.

Many properties and methods are HTMLCollection objects (which are actually read-only arrays that can be used with array or name index), where references to anchors, forms, links, and other scriptable elements are stored.

These collection properties all originate from the 0-level DOM. They have been Document.getElementsByTagName() has been replaced, but is still often used because it is convenient.

write() methodIt is noteworthy that during document loading and parsing, it allows a script to insert dynamically generated content into the document.

Note, in 1st level DOM, HTMLDocument defines a method named getElementById() A very useful method. In 2nd level DOM, this method has been moved to the Document interface, and it is now inherited by HTMLDocument instead of being defined by it.