Document
Baseline Widely available *
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
* Some parts of this feature may have varying levels of support.
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 The Creates a new This interface also inherits from the Returns the Add an array of constructed stylesheets to be used by the document.
These stylesheets may also be shared with shadow DOM subtrees of the same document. Returns the character set being used by the document. Returns the number of child elements of the current document. Returns the child elements of the current document. Indicates whether the document is rendered in quirks or strict mode. Returns the Content-Type from the MIME Header of the current document. Returns the Returns the Document Type Definition (DTD) of the current document. Returns the Returns the document location as a string. Returns an Returns the Returns the first child element of the current document. Returns the Returns an Returns the The element that's currently in full screen mode for this document. Returns a Boolean value indicating if the page is considered hidden or not. Returns an Returns the DOM implementation associated with the current document. Returns the last child element of the current document. Returns an Returns the Returns true if the picture-in-picture feature is enabled. Returns an Returns the element set as the target for mouse events while the pointer is locked. Returns a boolean that indicates whether the document is currently in the process of prerendering, as initiated via the Speculation Rules API. Returns an Returns a reference to the Returns a Returns timeline as a special instance of Returns a The Returns a semicolon-separated list of the cookies for that document or sets a single cookie. Returns a reference to the window object. Gets/sets the ability to edit the whole document. Gets/sets directionality (rtl/ltr) of the document. Indicates whether fullscreen mode is available. Returns the date on which the document was last modified. Returns the URI of the current document. Returns loading status of the document. Returns the URI of the page that linked to this page. Sets or gets the title of the current document. Returns the document location as a string. Returns or sets the color of active links in the document body. Provides access to all elements in the document — it returns an Returns a list of all of the anchors in the document. Returns an empty Gets/sets the background color of the current document. Alias of Gets/sets the domain of the current document. Gets/sets the foreground color, or text color, of the current document. Returns Alias of Returns the name of the style sheet set that was last enabled. Has the value Gets/sets the color of hyperlinks in the document. Returns the preferred style sheet set as specified by the page author. Like Returns which style sheet set is currently in use. Returns a list of the style sheet sets available on the document. Gets/sets the color of visited hyperlinks. Returns the encoding as determined by the XML declaration. Returns Returns the version number as specified in the XML declaration or This interface also inherits from the Adopt node from an external document. Inserts a set of Returns a promise that fulfills with an array of objects representing the top topics for the user, one from each of the last three epochs. By default, the method also causes the browser to record the current page visit as observed by the caller, so the page's hostname can later be used in topics calculation. See the Topics API for more details. See Returns a Gets a Creates a new Creates a new attribute node in a given namespace and returns it. Creates a new CDATA node and returns it. Creates a new comment node and returns it. Creates a new document fragment. Creates a new element with the given tag name. Creates a new element with the given tag name and namespace URI. Creates an event object. Creates a Creates a new Creates a Creates a text node. Creates a Creates a Creates a Returns the topmost element at the specified coordinates. Returns an array of all elements at the specified coordinates. Enables the style sheets for the specified style sheet set. Stops document's fullscreen element from being displayed fullscreen. Remove the video from the floating picture-in-picture window back to its original container. Release the pointer lock. Returns an array of all Returns a list of Returns an object reference to the identified element. Returns a list of elements with the given class name. Returns a list of elements with the given tag name. Returns a list of elements with the given tag name and namespace. Returns a Returns a New name for Returns a clone of a node from an external document. Moves a given Allows you to change the element being used as the background image for a specified element ID. Inserts a set of Returns the first Element node within the document, in document order, that matches the specified selectors. Returns a list of all the Element nodes within the document that match the specified selectors. Releases the current mouse capture if it's on an element in this document. Replaces the existing children of a document with a specified new set of children. Allows a document loaded in a third-party context (i.e., embedded in an Allows top-level sites to request third-party cookie access on behalf of embedded content originating from another site in the same related website set. Starts a new view transition and returns a The Compiles an Returns the input node as-is. Evaluates an XPath expression. The This method does nothing. Closes a document stream for writing. On an editable document, executes a formatting command. Returns a list of elements with the given name. Returns Opens a document stream for writing. Returns true if the formatting command can be executed on the current range. Returns true if the formatting command is in an indeterminate state on the current range. Returns true if the formatting command has been executed on the current range. Returns true if the formatting command is supported on the current range. Returns the current value of the current range for a formatting command. Writes text in a document. Writes a line of text in a document. This interface also inherits from the Creates a new Listen to these events using Fired on a prerendered document when it is activated (i.e., the user views the page). Fired when a content security policy is violated. Fired when the content of a tab has become visible or has been hidden. Fired when the Fired if an error occurs while attempting to switch into or out of fullscreen mode. Fired when the document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading. Fired when the Fired when the pointer is locked/unlocked. Fired when locking the pointer failed. Fired when the document view or an element has been scrolled. Fired when the document view or an element has completed scrolling. Fired on the scroll container at the end of a scrolling operation when a new scroll snap target has been selected. Fired on the scroll container when the browser determines a new scroll snap target is pending, i.e., it will be selected when the current scroll gesture ends. Fired when the current text selection on a document is changed. Not all events that bubble can reach the and
, 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.
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.Constructor
Document()
Document
object.Instance properties
Node
and EventTarget
interfaces.
Document.activeElement
Read onlyElement
that currently has focus.Document.adoptedStyleSheets
Document.body
Document.characterSet
Read onlyDocument.childElementCount
Read onlyDocument.children
Read onlyDocument.compatMode
Read onlyDocument.contentType
Read onlyDocument.currentScript
Read only element whose script is currently being processed and isn't a JavaScript module.
Document.doctype
Read onlyDocument.documentElement
Read onlyElement
that is a direct child of the document. For HTML documents, this is normally the HTMLHtmlElement
object representing the document's element.
Document.documentURI
Read onlyDocument.embeds
Read onlyHTMLCollection
of the embedded elements in the document.
Document.featurePolicy
Experimental
Read onlyFeaturePolicy
interface with the feature policies applied to the document.Document.firstElementChild
Read onlyDocument.fonts
FontFaceSet
interface of the current document.Document.forms
Read onlyHTMLCollection
of the elements in the document.
Document.fragmentDirective
Read onlyFragmentDirective
for the current document.Document.fullscreenElement
Read onlyDocument.head
Read onlyDocument.images
Read onlyHTMLCollection
of the images in the document.Document.implementation
Read onlyDocument.lastElementChild
Read onlyDocument.links
Read onlyHTMLCollection
of the hyperlinks in the document.Document.pictureInPictureElement
Read onlyElement
currently being presented in picture-in-picture mode in this document.Document.pictureInPictureEnabled
Read onlyDocument.plugins
Read onlyHTMLCollection
of the available plugins.Document.pointerLockElement
Read onlynull
if lock is pending, pointer is unlocked, or if the target is in another document.Document.prerendering
Read only
Experimental
Document.scripts
Read onlyHTMLCollection
of the elements in the document.
Document.scrollingElement
Read onlyElement
that scrolls the document.Document.styleSheets
Read onlyStyleSheetList
of CSSStyleSheet
objects for stylesheets explicitly linked into, or embedded in a document.Document.timeline
Read onlyDocumentTimeline
that is automatically created on page load.Document.visibilityState
Read onlystring
denoting the visibility state of the document. Possible values are visible
, hidden
, prerender
, and unloaded
.Extensions for HTMLDocument
Document
interface for HTML documents inherits from the HTMLDocument
interface or is extended for such documents.
Document.defaultView
Read onlyDocument.designMode
Document.dir
Document.fullscreenEnabled
Read onlyDocument.lastModified
Read onlyDocument.location
Read onlyDocument.readyState
Read onlyDocument.referrer
Read onlyDocument.title
Document.URL
Read onlyDeprecated properties
Document.alinkColor
Deprecated
Document.all
Deprecated
HTMLAllCollection
rooted at the document node. This is a legacy, non-standard property and should not be used.Document.anchors
Deprecated
Read onlyDocument.applets
Deprecated
Read onlyHTMLCollection
. Legacy property that used to return the list of applets within a document.Document.bgColor
Deprecated
Document.charset
Deprecated
Read onlyDocument.characterSet
. Use this property instead.Document.domain
Deprecated
Document.fgColor
Deprecated
Document.fullscreen
Deprecated
true
when the document is in fullscreen mode.Document.inputEncoding
Deprecated
Read onlyDocument.characterSet
. Use this property instead.Document.lastStyleSheetSet
Deprecated
Read only
Non-standard
null
until the style sheet is changed by setting the value of selectedStyleSheetSet
.Document.linkColor
Deprecated
Document.preferredStyleSheetSet
Deprecated
Read only
Non-standard
Document.rootElement
Deprecated
Document.documentElement
, but only for root elements. Use this property instead.
Document.selectedStyleSheetSet
Deprecated
Non-standard
Document.styleSheetSets
Deprecated
Read only
Non-standard
Document.vlinkColor
Deprecated
Document.xmlEncoding
Deprecated
Document.xmlStandalone
Deprecated
true
if the XML declaration specifies the document to be standalone (e.g., An external part of the DTD affects the document's content), else false
.Document.xmlVersion
Deprecated
"1.0"
if the declaration is absent.Instance methods
Node
and EventTarget
interfaces.
Document.adoptNode()
Document.append()
Node
objects or strings after the last child of the document.Document.browsingTopics()
Experimental
Non-standard
Document.captureEvents()
Deprecated
Window.captureEvents
.Document.caretPositionFromPoint()
CaretPosition
object containing the DOM node containing the caret, and caret's character offset within that node.Document.caretRangeFromPoint()
Non-standard
Range
object for the document fragment under the specified coordinates.Document.createAttribute()
Attr
object and returns it.Document.createAttributeNS()
Document.createCDATASection()
Document.createComment()
Document.createDocumentFragment()
Document.createElement()
Document.createElementNS()
Document.createEvent()
Deprecated
Document.createNodeIterator()
NodeIterator
object.Document.createProcessingInstruction()
ProcessingInstruction
object.Document.createRange()
Range
object.Document.createTextNode()
Document.createTouch()
Deprecated
Non-standard
Touch
object.Document.createTouchList()
Deprecated
Non-standard
TouchList
object.Document.createTreeWalker()
TreeWalker
object.Document.elementFromPoint()
Document.elementsFromPoint()
Document.enableStyleSheetsForSet()
Deprecated
Non-standard
Document.exitFullscreen()
Document.exitPictureInPicture()
Document.exitPointerLock()
Document.getAnimations()
Animation
objects currently in effect, whose target elements are descendants of the document
.Document.getBoxQuads()
Experimental
DOMQuad
objects representing the CSS fragments of the node.Document.getElementById()
Document.getElementsByClassName()
Document.getElementsByTagName()
Document.getElementsByTagNameNS()
Document.getSelection()
Selection
object representing the range of text selected by the user, or the current position of the caret.Document.hasStorageAccess()
Promise
that resolves with a boolean value indicating whether the document has access to unpartitioned cookies.Document.hasStorageAccess()
.Document.importNode()
Document.moveBefore()
Experimental
Node
inside the Document
DOM node as a direct child, before a given reference node, without removing and then inserting the node.Document.mozSetImageElement()
Non-standard
Document.prepend()
Node
objects or strings before the first child of the document.Document.querySelector()
Document.querySelectorAll()
Document.releaseCapture()
Non-standard
Document.releaseEvents()
Deprecated
Document.replaceChildren()
Document.requestStorageAccess()
) 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.requestStorageAccessFor()
Experimental
Document.startViewTransition()
ViewTransition
object to represent it.Document
interface is extended with the XPathEvaluator
interface:
Document.createExpression()
XPathExpression
which can then be used for (repeated) evaluations.Document.createNSResolver()
Deprecated
Document.evaluate()
Extension for HTML documents
Document
interface for HTML documents inherit from the HTMLDocument
interface or is extended for such documents:
Document.clear()
Deprecated
Document.close()
Document.execCommand()
Deprecated
Document.getElementsByName()
Document.hasFocus()
true
if the focus is currently located anywhere inside the specified document.Document.open()
Document.queryCommandEnabled()
Deprecated
Non-standard
Document.queryCommandIndeterm()
Deprecated
Document.queryCommandState()
Deprecated
Non-standard
Document.queryCommandSupported()
Deprecated
Non-standard
Document.queryCommandValue()
Deprecated
Document.write()
Deprecated
Document.writeln()
Static methods
Node
and EventTarget
interfaces.
Document.parseHTMLUnsafe()
Document
object from a string of HTML without performing sanitization.
The string may contain declarative shadow roots.Events
addEventListener()
or by assigning an event listener to the oneventname
property of this interface. In addition to the events listed below, many events can bubble from nodes contained in the document tree.
afterscriptexecute
Non-standard
Deprecated
beforescriptexecute
Non-standard
Deprecated
prerenderingchange
Experimental
securitypolicyviolation
visibilitychange
Fullscreen events
fullscreenchange
Document
transitions into or out of fullscreen mode.fullscreenerror
Load & unload events
DOMContentLoaded
readystatechange
readyState
attribute of a document has changed.Pointer lock events
pointerlockchange
pointerlockerror
Scroll events
scroll
scrollend
scrollsnapchange
Experimental
scrollsnapchanging
Experimental
Selection events
selectionchange
Bubbled events
Document
object. Only the following do and can be listened for on the Document
object:
abort
auxclick
beforeinput
beforematch
beforetoggle
blur
cancel
canplay
canplaythrough
change
click
close
contextlost
contextmenu
contextrestored
copy
cuechange
cut
dblclick
drag
dragend
dragenter
dragleave
dragover
dragstart
drop
durationchange
emptied
ended
error
focus
formdata
input
invalid
keydown
keypress
keyup
load
loadeddata
loadedmetadata
loadstart
mousedown
mouseenter
mouseleave
mousemove
mouseout
mouseover
mouseup
paste
pause
play
playing
progress
ratechange
reset
resize
scroll
scrollend
securitypolicyviolation
seeked
seeking
select
slotchange
stalled
submit
suspend
timeupdate
toggle
volumechange
waiting
wheel
Specifications
Browser compatibility