Document extension type

The Document interface represents any web page loaded in the browser and serves as an entry point into the web page's content, which is the DOM tree.

The DOM tree includes elements such as body and table, among many others. It provides functionality globally to the document, like how to obtain the page's URL and create new elements in the document.

The Document interface describes the common properties and methods for any kind of document. Depending on the document's type (e.g. HTML, XML, SVG, …), a larger API is available: HTML documents, served with the "text/html" content type, also implement the HTMLDocument interface, whereas XML and SVG documents implement the XMLDocument interface.

on
Implemented types
Implementers

Constructors

Document()
factory

Properties

activeElement Element?
no setter
adoptedStyleSheets JSArray<CSSStyleSheet>
getter/setter pair
alinkColor String
getter/setter pair
all HTMLAllCollection
no setter
anchors HTMLCollection
no setter
applets HTMLCollection
no setter
baseURI String
no setterinherited
bgColor String
getter/setter pair
body HTMLElement?
getter/setter pair
characterSet String
no setter
charset String
no setter
childElementCount int
no setter
childNodes NodeList
no setterinherited
children HTMLCollection
no setter
compatMode String
no setter
contentType String
no setter
getter/setter pair
currentScript HTMLOrSVGScriptElement?
no setter
defaultView Window?
no setter
designMode String
getter/setter pair
dir String
getter/setter pair
doctype DocumentType?
no setter
documentElement Element?
no setter
documentURI String
no setter
domain String
getter/setter pair
embeds HTMLCollection
no setter
fgColor String
getter/setter pair
firstChild Node?
no setterinherited
firstElementChild Element?
no setter
fonts FontFaceSet
no setter
forms HTMLCollection
no setter
hashCode int
The hash code for this object.
no setterinherited
no setter
hidden bool
no setter
images HTMLCollection
no setter
implementation DOMImplementation
no setter
inputEncoding String
no setter
isConnected bool
no setterinherited
lastChild Node?
no setterinherited
lastElementChild Element?
no setter
lastModified String
no setter
linkColor String
getter/setter pair
no setter
location Location?
no setter
nextSibling Node?
no setterinherited
nodeName String
no setterinherited
nodeType int
no setterinherited
nodeValue String?
getter/setter pairinherited
onabort EventHandler?
getter/setter pair
onanimationcancel EventHandler?
getter/setter pair
onanimationend EventHandler?
getter/setter pair
onanimationiteration EventHandler?
getter/setter pair
onanimationstart EventHandler?
getter/setter pair
onauxclick EventHandler?
getter/setter pair
onbeforeinput EventHandler?
getter/setter pair
onbeforematch EventHandler?
getter/setter pair
onbeforetoggle EventHandler?
getter/setter pair
onblur EventHandler?
getter/setter pair
oncancel EventHandler?
getter/setter pair
oncanplay EventHandler?
getter/setter pair
oncanplaythrough EventHandler?
getter/setter pair
onchange EventHandler?
getter/setter pair
onclick EventHandler?
getter/setter pair
onclose EventHandler?
getter/setter pair
oncontextlost EventHandler?
getter/setter pair
oncontextmenu EventHandler?
getter/setter pair
oncontextrestored EventHandler?
getter/setter pair
oncopy EventHandler?
getter/setter pair
oncuechange EventHandler?
getter/setter pair
oncut EventHandler?
getter/setter pair
ondblclick EventHandler?
getter/setter pair
ondrag EventHandler?
getter/setter pair
ondragend EventHandler?
getter/setter pair
ondragenter EventHandler?
getter/setter pair
ondragleave EventHandler?
getter/setter pair
ondragover EventHandler?
getter/setter pair
ondragstart EventHandler?
getter/setter pair
ondrop EventHandler?
getter/setter pair
ondurationchange EventHandler?
getter/setter pair
onemptied EventHandler?
getter/setter pair
onended EventHandler?
getter/setter pair
onerror OnErrorEventHandler?
getter/setter pair
onfocus EventHandler?
getter/setter pair
onformdata EventHandler?
getter/setter pair
ongotpointercapture EventHandler?
getter/setter pair
oninput EventHandler?
getter/setter pair
oninvalid EventHandler?
getter/setter pair
onkeydown EventHandler?
getter/setter pair
onkeypress EventHandler?
getter/setter pair
onkeyup EventHandler?
getter/setter pair
onload EventHandler?
getter/setter pair
onloadeddata EventHandler?
getter/setter pair
onloadedmetadata EventHandler?
getter/setter pair
onloadstart EventHandler?
getter/setter pair
onlostpointercapture EventHandler?
getter/setter pair
onmousedown EventHandler?
getter/setter pair
onmouseenter EventHandler?
getter/setter pair
onmouseleave EventHandler?
getter/setter pair
onmousemove EventHandler?
getter/setter pair
onmouseout EventHandler?
getter/setter pair
onmouseover EventHandler?
getter/setter pair
onmouseup EventHandler?
getter/setter pair
onpaste EventHandler?
getter/setter pair
onpause EventHandler?
getter/setter pair
onplay EventHandler?
getter/setter pair
onplaying EventHandler?
getter/setter pair
onpointercancel EventHandler?
getter/setter pair
onpointerdown EventHandler?
getter/setter pair
onpointerenter EventHandler?
getter/setter pair
onpointerleave EventHandler?
getter/setter pair
onpointermove EventHandler?
getter/setter pair
onpointerout EventHandler?
getter/setter pair
onpointerover EventHandler?
getter/setter pair
onpointerrawupdate EventHandler?
getter/setter pair
onpointerup EventHandler?
getter/setter pair
onprogress EventHandler?
getter/setter pair
onratechange EventHandler?
getter/setter pair
onreadystatechange EventHandler?
getter/setter pair
onreset EventHandler?
getter/setter pair
onresize EventHandler?
getter/setter pair
onscroll EventHandler?
getter/setter pair
onscrollend EventHandler?
getter/setter pair
onsecuritypolicyviolation EventHandler?
getter/setter pair
onseeked EventHandler?
getter/setter pair
onseeking EventHandler?
getter/setter pair
onselect EventHandler?
getter/setter pair
onselectionchange EventHandler?
getter/setter pair
onselectstart EventHandler?
getter/setter pair
onslotchange EventHandler?
getter/setter pair
onstalled EventHandler?
getter/setter pair
onsubmit EventHandler?
getter/setter pair
onsuspend EventHandler?
getter/setter pair
ontimeupdate EventHandler?
getter/setter pair
ontoggle EventHandler?
getter/setter pair
ontouchcancel EventHandler?
getter/setter pair
ontouchend EventHandler?
getter/setter pair
ontouchmove EventHandler?
getter/setter pair
ontouchstart EventHandler?
getter/setter pair
ontransitioncancel EventHandler?
getter/setter pair
ontransitionend EventHandler?
getter/setter pair
ontransitionrun EventHandler?
getter/setter pair
ontransitionstart EventHandler?
getter/setter pair
onvisibilitychange EventHandler?
getter/setter pair
onvolumechange EventHandler?
getter/setter pair
onwaiting EventHandler?
getter/setter pair
onwebkitanimationend EventHandler?
getter/setter pair
onwebkitanimationiteration EventHandler?
getter/setter pair
onwebkitanimationstart EventHandler?
getter/setter pair
onwebkittransitionend EventHandler?
getter/setter pair
onwheel EventHandler?
getter/setter pair
ownerDocument Document?
no setterinherited
parentElement Element?
no setterinherited
parentNode Node?
no setterinherited
plugins HTMLCollection
no setter
previousSibling Node?
no setterinherited
readyState DocumentReadyState
no setter
referrer String
no setter
rootElement SVGSVGElement?
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
scripts HTMLCollection
no setter
scrollingElement Element?
no setter
styleSheets StyleSheetList
no setter
textContent String?
getter/setter pairinherited
timeline DocumentTimeline
no setter
title String
getter/setter pair
URL String
no setter
visibilityState DocumentVisibilityState
no setter
vlinkColor String
getter/setter pair

Methods

addEventListener(String type, EventListener? callback, [JSAny options]) → void
The addEventListener() method of the EventTarget interface sets up a function that will be called whenever the specified event is delivered to the target.
inherited
adoptNode(Node node) Node
Document.adoptNode() transfers a from another Document into the method's document. The adopted node and its subtree are removed from their original document (if any), and their Node.ownerDocument is changed to the current document. The node can then be inserted into the current document.
append(JSAny nodes) → void
The Document.append() method inserts a set of Node objects or string objects after the last child of the document. String objects are inserted as equivalent Text nodes.
appendChild(Node node) Node
The appendChild() method of the Node interface adds a node to the end of the list of children of a specified parent node.
inherited
captureEvents() → void
caretPositionFromPoint(num x, num y) CaretPosition?
The caretPositionFromPoint() method of the Document interface returns a CaretPosition object, containing the DOM node, along with the caret and caret's character offset within that node.
clear() → void
The Document.clear() method does nothing, but doesn't raise any error.
cloneNode([bool deep]) Node
The cloneNode() method of the Node interface returns a duplicate of the node on which this method was called. Its parameter controls if the subtree contained in a node is also cloned or not.
inherited
close() → void
The Document.close() method finishes writing to a document, opened with Document.open.
compareDocumentPosition(Node other) int
The compareDocumentPosition() method of the Node interface reports the position of its argument node relative to the node on which it is called.
inherited
contains(Node? other) bool
The contains() method of the Node interface returns a boolean value indicating whether a node is a descendant of a given node, that is the node itself, one of its direct children (Node.childNodes), one of the children's direct children, and so on.
inherited
convertPointFromNode(DOMPointInit point, GeometryNode from, [ConvertCoordinateOptions options]) DOMPoint
convertQuadFromNode(DOMQuadInit quad, GeometryNode from, [ConvertCoordinateOptions options]) DOMQuad
convertRectFromNode(DOMRectReadOnly rect, GeometryNode from, [ConvertCoordinateOptions options]) DOMQuad
createAttribute(String localName) Attr
The Document.createAttribute() method creates a new attribute node, and returns it. The object created is a node implementing the Attr interface. The DOM does not enforce what sort of attributes can be added to a particular element in this manner.
createAttributeNS(String? namespace, String qualifiedName) Attr
The Document.createAttributeNS() method creates a new attribute node with the specified namespace URI and qualified name, and returns it. The object created is a node implementing the Attr interface. The DOM does not enforce what sort of attributes can be added to a particular element in this manner.
createCDATASection(String data) CDATASection
createCDATASection() creates a new CDATA section node, and returns it.
createComment(String data) Comment
createComment() creates a new comment node, and returns it.
createDocumentFragment() DocumentFragment
Creates a new empty DocumentFragment into which DOM nodes can be added to build an offscreen DOM tree.
createElement(String localName, [JSAny options]) Element
In an HTML document, the document.createElement() method creates the HTML element specified by tagName, or an HTMLUnknownElement if tagName isn't recognized.
createElementNS(String? namespace, String qualifiedName, [JSAny options]) Element
Creates an element with the specified namespace URI and qualified name.
createEvent(String interface) Event

Warning: Many methods used with createEvent, such as initCustomEvent, are deprecated. Use event constructors instead.

createExpression(String expression, [XPathNSResolver? resolver]) XPathExpression
This method compiles an XPathExpression which can then be used for (repeated) evaluations.
createNodeIterator(Node root, [int whatToShow, NodeFilter? filter]) NodeIterator
The Document.createNodeIterator() method returns a new NodeIterator object.
createNSResolver(Node nodeResolver) Node
Creates an XPathNSResolver which resolves namespaces with respect to the definitions in scope for a specified node.
createProcessingInstruction(String target, String data) ProcessingInstruction
createProcessingInstruction() generates a new processing instruction node and returns it.
createRange() Range
The Document.createRange() method returns a new Range object.
createTextNode(String data) Text
Creates a new Text node. This method can be used to escape HTML characters.
createTreeWalker(Node root, [int whatToShow, NodeFilter? filter]) TreeWalker
The Document.createTreeWalker() creator method returns a newly created TreeWalker object.
dispatchEvent(Event event) bool
The dispatchEvent() method of the EventTarget sends an Event to the object, (synchronously) invoking the affected event listeners in the appropriate order. The normal event processing rules (including the capturing and optional bubbling phase) also apply to events dispatched manually with dispatchEvent().
inherited
elementFromPoint(num x, num y) Element?
The elementFromPoint() method, available on the Document object, returns the topmost Element at the specified coordinates (relative to the viewport).
elementsFromPoint(num x, num y) JSArray<Element>
The elementsFromPoint() method of the Document interface returns an array of all elements at the specified coordinates (relative to the viewport). The elements are ordered from the topmost to the bottommost box of the viewport.
evaluate(String expression, Node contextNode, [XPathNSResolver? resolver, int type, XPathResult? result]) XPathResult
The evaluate() method of the Document interface selects elements based on the XPath expression given in parameters.
execCommand(String commandId, [bool showUI, String value]) bool
The execCommand method implements multiple different commands. Some of them provide access to the clipboard, while others are for editing form inputs, contenteditable elements or entire documents (when switched to design mode).
getAnimations() JSArray<Animation>
The getAnimations() method of the Document interface returns an array of all Animation objects currently in effect whose target elements are descendants of the document. This array includes CSS Animations, CSS Transitions, and Web Animations.
getBoxQuads([BoxQuadOptions options]) JSArray<DOMQuad>
getElementById(String elementId) Element?
The getElementById() method of the Document interface returns an Element object representing the element whose Element.id property matches the specified string. Since element IDs are required to be unique if specified, they're a useful way to get access to a specific element quickly.
getElementsByClassName(String classNames) HTMLCollection
The getElementsByClassName method of Document interface returns an array-like object of all child elements which have all of the given class name(s).
getElementsByName(String elementName) NodeList
The getElementsByName() method of the Document object returns a NodeList Collection of elements with a given name attribute in the document.
getElementsByTagName(String qualifiedName) HTMLCollection
The getElementsByTagName method of Document interface returns an HTMLCollection of elements with the given tag name.
getElementsByTagNameNS(String? namespace, String localName) HTMLCollection
Returns a list of elements with the given tag name belonging to the given namespace. The complete document is searched, including the root node.
getRootNode([GetRootNodeOptions options]) Node
The getRootNode() method of the Node interface returns the context object's root, which optionally includes the shadow root if it is available.
inherited
getSelection() Selection?
The getSelection() method of the Document interface returns a Selection object representing the range of text selected by the user, or the current position of the caret.
hasChildNodes() bool
The hasChildNodes() method of the Node interface returns a boolean value indicating whether the given Node has child nodes or not.
inherited
hasFocus() bool
The hasFocus() method of the Document interface returns a boolean value indicating whether the document or any element inside the document has focus. This method can be used to determine whether the active element in a document has focus.
importNode(Node node, [bool deep]) Node
The Document object's importNode() method creates a copy of a Node or DocumentFragment from another document, to be inserted into the current document later.
insertBefore(Node node, Node? child) Node
The insertBefore() method of the Node interface inserts a node before a reference node as a child of a specified parent node.
inherited
isDefaultNamespace(String? namespace) bool
The isDefaultNamespace() method of the Node interface accepts a namespace URI as an argument. It returns a boolean value that is true if the namespace is the default namespace on the given node and false if not.
inherited
isEqualNode(Node? otherNode) bool
The isEqualNode() method of the Node interface tests whether two nodes are equal. Two nodes are equal when they have the same type, defining characteristics (for elements, this would be their ID, number of children, and so forth), its attributes match, and so on. The specific set of data points that must match varies depending on the types of the nodes.
inherited
isSameNode(Node? otherNode) bool
The isSameNode() method of the Node interface is a legacy alias the for the === strict equality operator. That is, it tests whether two nodes are the same (in other words, whether they reference the same object).
inherited
lookupNamespaceURI(String? prefix) String?
The lookupNamespaceURI() method of the Node interface takes a prefix as parameter and returns the namespace URI associated with it on the given node if found (and null if not).
inherited
lookupPrefix(String? namespace) String?
The lookupPrefix() method of the Node interface returns a string containing the prefix for a given namespace URI, if present, and null if not. When multiple prefixes are possible, the first prefix is returned.
inherited
normalize() → void
The normalize() method of the Node interface puts the specified node and all of its sub-tree into a normalized form. In a normalized sub-tree, no text nodes in the sub-tree are empty and there are no adjacent text nodes.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
open([String unused1OrUrl, String nameOrUnused2, String features]) JSObject?
The Document.open() method opens a document for Document.write.
prepend(JSAny nodes) → void
The Document.prepend() method inserts a set of Node objects or string objects before the first child of the document. String objects are inserted as equivalent Text nodes.
queryCommandEnabled(String commandId) bool
The Document.queryCommandEnabled() method reports whether or not the specified editor command is enabled by the browser.
queryCommandIndeterm(String commandId) bool
queryCommandState(String commandId) bool
The queryCommandState() method will tell you if the current selection has a certain Document.execCommand command applied.
queryCommandSupported(String commandId) bool
The Document.queryCommandSupported() method reports whether or not the specified editor command is supported by the browser.
queryCommandValue(String commandId) String
querySelector(String selectors) Element?
The Document method querySelector() returns the first Element within the document that matches the specified selector, or group of selectors. If no matches are found, null is returned.
querySelectorAll(String selectors) NodeList
The Document method querySelectorAll() returns a static (not live) NodeList representing a list of the document's elements that match the specified group of selectors.
releaseEvents() → void
removeChild(Node child) Node
The removeChild() method of the Node interface removes a child node from the DOM and returns the removed node.
inherited
removeEventListener(String type, EventListener? callback, [JSAny options]) → void
The removeEventListener() method of the EventTarget interface removes an event listener previously registered with EventTarget.addEventListener from the target. The event listener to be removed is identified using a combination of the event type, the event listener function itself, and various optional options that may affect the matching process; see Matching event listeners for removal.
inherited
replaceChild(Node node, Node child) Node
The replaceChild() method of the Node interface replaces a child node within the given (parent) node.
inherited
replaceChildren(JSAny nodes) → void
The Document.replaceChildren() method replaces the existing children of a Document with a specified new set of children.
toString() String
A string representation of this object.
inherited
write(String text) → void

Warning: Use of the document.write() method is strongly discouraged.

As the HTML spec itself warns:

This method has very idiosyncratic behavior. In some cases, this method can affect the state of the

HTML parser while the parser is running, resulting in a DOM that does not correspond to the source of the document (e.g. if the string written is the string "<plaintext>" or "<!--"). In other cases, the call can clear the current page first, as if document.open() had been called. In yet more cases, the method is simply ignored, or throws an exception. Users agents are explicitly allowed to avoid executing script elements inserted via this method. And to make matters even worse, the exact behavior of this method can in some cases be dependent on network latency, which can lead to failures that are very hard to debug. For all these reasons, use of this method is strongly discouraged.

Therefore, avoid using document.write() — and if possible, update any existing code that is still using it.

writeln(String text) → void

Warning: Use of the document.writeln() method is strongly discouraged.

As the HTML spec itself warns:

This method has very idiosyncratic behavior. In some cases, this method can affect the state of the

HTML parser while the parser is running, resulting in a DOM that does not correspond to the source of the document (e.g. if the string written is the string "<plaintext>" or "<!--"). In other cases, the call can clear the current page first, as if document.open() had been called. In yet more cases, the method is simply ignored, or throws an exception. Users agents are explicitly allowed to avoid executing script elements inserted via this method. And to make matters even worse, the exact behavior of this method can in some cases be dependent on network latency, which can lead to failures that are very hard to debug. For all these reasons, use of this method is strongly discouraged.

Therefore, avoid using document.writeln() — and if possible, update any existing code that is still using it.

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

parseHTMLUnsafe(String html) Document