Document Interface

The Document interface is an interface for web documents. It represents a document as a logical tree and has different properties and methods you can access as you build. 

Instance methods

This interface also inherits from the Node and EventTarget interfaces.

Document.adoptNode()

Adopt node from an external document.

Document.append()

Inserts a set of Node objects or string objects after the last child of the document.

Document.captureEvents() Deprecated

See Window.captureEvents.

Document.caretPositionFromPoint()

Returns a CaretPosition object containing the DOM node containing the caret, and caret's character offset within that node.

Document.caretRangeFromPoint() Non-standard

Gets a Range object for the document fragment under the specified coordinates.

Document.createAttribute()

Creates a new Attr object and returns it.

Document.createAttributeNS()

Creates a new attribute node in a given namespace and returns it.

Document.createCDATASection()

Creates a new CDATA node and returns it.

Document.createComment()

Creates a new comment node and returns it.

Document.createDocumentFragment()

Creates a new document fragment.

Document.createElement()

Creates a new element with the given tag name.

Document.createElementNS()

Creates a new element with the given tag name and namespace URI.

Document.createEntityReference() Deprecated

Creates a new entity reference object and returns it.

Document.createEvent()

Creates an event object.

Document.createNodeIterator()

Creates a NodeIterator object.

Document.createProcessingInstruction()

Creates a new ProcessingInstruction object.

Document.createRange()

Creates a Range object.

Document.createTextNode()

Creates a text node.

Document.createTouch() Deprecated Non-standard

Creates a Touch object.

Document.createTouchList() Deprecated Non-standard

Creates a TouchList object.

Document.createTreeWalker()

Creates a TreeWalker object.

Document.elementFromPoint()

Returns the topmost element at the specified coordinates.

Document.elementsFromPoint()

Returns an array of all elements at the specified coordinates.

Document.enableStyleSheetsForSet() Deprecated Non-standard

Enables the style sheets for the specified style sheet set.

Document.exitFullscreen()

Stops document's fullscreen element from being displayed fullscreen.

Document.exitPictureInPicture()

Remove the video from the floating picture-in-picture window back to its original container.

Document.exitPointerLock()

Release the pointer lock.

Document.getAnimations()

Returns an array of all Animation objects currently in effect, whose target elements are descendants of the document.

Document.getBoxQuads() Experimental

Returns a list of DOMQuad objects representing the CSS fragments of the node.

Document.getElementById()

Returns an object reference to the identified element.

Document.getElementsByClassName()

Returns a list of elements with the given class name.

Document.getElementsByTagName()

Returns a list of elements with the given tag name.

Document.getElementsByTagNameNS()

Returns a list of elements with the given tag name and namespace.

Document.getSelection()

Returns a Selection object representing the range of text selected by the user, or the current position of the caret.

Document.hasStorageAccess()

Returns a Promise that resolves with a boolean value indicating whether the document has access to unpartitioned cookies.

Document.importNode()

Returns a clone of a node from an external document.

Document.prepend()

Inserts a set of Node objects or string objects before the first child of the document.

Document.querySelector()

Returns the first Element node within the document, in document order, that matches the specified selectors.

Document.querySelectorAll()

Returns a list of all the Element nodes within the document that match the specified selectors.

Document.releaseCapture() Non-standard

Releases the current mouse capture if it's on an element in this document.

Document.releaseEvents() Deprecated

See Window.releaseEvents().

Document.replaceChildren()

Replaces the existing children of a document with a specified new set of children.

Document.requestStorageAccess()

Allows a document loaded in a third-party context (i.e. embedded in an <iframe>) to request access to unpartitioned cookies, in cases where user agents by default block access to unpartitioned cookies by sites loaded in a third-party context to improve privacy.

Document.startViewTransition() Experimental

Starts a new view transition and returns a ViewTransition object to represent it.

Document.mozSetImageElement() Non-standard

Allows you to change the element being used as the background image for a specified element ID.

The Document interface is extended with the XPathEvaluator interface:

Document.createExpression()

Compiles an XPathExpression which can then be used for (repeated) evaluations.

Document.createNSResolver()

Creates an XPathNSResolver object.

Document.evaluate()

Evaluates an XPath expression.


Extension for HTML documents

The Document interface for HTML documents inherit from the HTMLDocument interface or is extended for such documents:

Document.clear() Deprecated

This method does nothing.

Document.close()

Closes a document stream for writing.

Document.execCommand() Deprecated

On an editable document, executes a formatting command.

Document.getElementsByName()

Returns a list of elements with the given name.

Document.hasFocus()

Returns true if the focus is currently located anywhere inside the specified document.

Document.open()

Opens a document stream for writing.

Document.queryCommandEnabled() Deprecated Non-standard

Returns true if the formatting command can be executed on the current range.

Document.queryCommandIndeterm() Deprecated

Returns true if the formatting command is in an indeterminate state on the current range.

Document.queryCommandState() Deprecated Non-standard

Returns true if the formatting command has been executed on the current range.

Document.queryCommandSupported() Deprecated Non-standard

Returns true if the formatting command is supported on the current range.

Document.queryCommandValue() Deprecated

Returns the current value of the current range for a formatting command.

Document.write()

Writes text in a document.

Document.writeln()

Writes a line of text in a document.