Firefox Tomorrow

web api interface

Document

View on MDN ↗

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.

Constructor

Instance properties

This interface also inherits from the Node and EventTarget interfaces.

Extensions for HTMLDocument

The Document interface for HTML documents inherits from the HTMLDocument interface or is extended for such documents.

  • cookie
    • : Returns a semicolon-separated list of the cookies for that document or sets a single cookie.
  • defaultView Read only
    • : Returns a reference to the window object.
  • designMode
    • : Gets/sets the ability to edit the whole document.
  • dir
    • : Gets/sets directionality (rtl/ltr) of the document.
  • fullscreenEnabled Read only
    • : Indicates whether fullscreen mode is available.
  • lastModified Read only
    • : Returns the date on which the document was last modified.
  • location Read only
    • : Returns the URI of the current document.
  • readyState Read only
    • : Returns loading status of the document.
  • referrer Read only
    • : Returns the URI of the page that linked to this page.
  • title
    • : Sets or gets the title of the current document.
  • URL Read only
    • : Returns the document location as a string.
  • Named properties
    • : Some elements in the document are also exposed as properties:

      • For each <embed>, <form>, <iframe>, <img>, and <object> element, its name (if non-empty) is exposed. For example, if the document contains <form name="my_form">, then document["my_form"] (and its equivalent document.my_form) returns a reference to that element.
      • For each <object> element, its id (if non-empty) is exposed.
      • For each <img> element with non-empty name, its id (if non-empty) is exposed.

      If a property corresponds to a single element, that element is directly returned. If that single element is an iframe, then its contentWindow is returned instead. If the property corresponds to multiple elements, then an HTMLCollection is returned containing all of them.

Deprecated properties

  • alinkColor Deprecated
    • : Returns or sets the color of active links in the document body.
  • all Deprecated
    • : Provides access to all elements in the document — it returns an HTMLAllCollection rooted at the document node. This is a legacy, non-standard property and should not be used.
  • anchors Deprecated Read only
    • : Returns a list of all of the anchors in the document.
  • applets Deprecated Read only
    • : Returns an empty HTMLCollection. Legacy property that used to return the list of applets within a document.
  • bgColor Deprecated
    • : Gets/sets the background color of the current document.
  • Document.charset Deprecated Read only
  • domain Deprecated
    • : Gets/sets the domain of the current document.
  • fgColor Deprecated
    • : Gets/sets the foreground color, or text color, of the current document.
  • fullscreen Deprecated
  • Document.inputEncoding Deprecated Read only
  • lastStyleSheetSet Deprecated Read only
    • : Returns the name of the style sheet set that was last enabled. Has the value null until the style sheet is changed by setting the value of selectedStyleSheetSet.
  • linkColor Deprecated
    • : Gets/sets the color of hyperlinks in the document.
  • preferredStyleSheetSet Deprecated Read only
    • : Returns the preferred style sheet set as specified by the page author.
  • rootElement Deprecated
  • selectedStyleSheetSet Deprecated
    • : Returns which style sheet set is currently in use.
  • styleSheetSets Deprecated Read only
    • : Returns a list of the style sheet sets available on the document.
  • vlinkColor Deprecated
    • : Gets/sets the color of visited hyperlinks.
  • xmlEncoding Deprecated
    • : Returns the encoding as determined by the XML declaration.
  • Document.xmlStandalone Deprecated
    • : Returns 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.
  • xmlVersion Deprecated
    • : Returns the version number as specified in the XML declaration or "1.0" if the declaration is absent.

Instance methods

This interface also inherits from the Node and EventTarget interfaces.

The Document interface is extended with the XPathEvaluator interface:

Extension for HTML documents

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

  • clear() Deprecated
    • : This method does nothing.
  • close()
    • : Closes a document stream for writing.
  • execCommand() Deprecated
    • : On an editable document, executes a formatting command.
  • getElementsByName()
    • : Returns a list of elements with the given name.
  • hasFocus()
    • : Returns true if the focus is currently located anywhere inside the specified document.
  • open()
    • : Opens a document stream for writing.
  • queryCommandEnabled() Deprecated
    • : 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.
  • queryCommandState() Deprecated
    • : Returns true if the formatting command has been executed on the current range.
  • queryCommandSupported() Deprecated
    • : 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.
  • write() Deprecated
    • : Writes text in a document.
  • writeln() Deprecated
    • : Writes a line of text in a document.

Static methods

This interface also inherits from the Node and EventTarget interfaces.

  • Document.parseHTML()
    • : Creates a new Document object from a string of HTML in an XSS-safe manner with sanitization.
  • Document.parseHTMLUnsafe()
    • : Creates a new Document object from a string of HTML without performing sanitization. The string may contain declarative shadow roots.

Events

Listen to these events using 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.

Fullscreen events

Load & unload events

  • DOMContentLoaded
    • : Fired when the document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading.
  • readystatechange
    • : Fired when the readyState attribute of a document has changed.

Pointer lock events

Scroll events

  • scroll
    • : Fired when the document view or an element has been scrolled.
  • scrollend
    • : Fired when the document view or an element has completed scrolling.
  • scrollsnapchange Experimental
    • : Fired on the scroll container at the end of a scrolling operation when a new scroll snap target has been selected.
  • scrollsnapchanging Experimental
    • : 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.

Selection events

  • selectionchange
    • : Fired when the current text selection on a document is changed.

Bubbled events

Not all events that bubble can reach the Document object. Only the following do and can be listened for on the Document object:

Specifications

SpecificationsStandards references are available on the canonical MDN page.

Browser compatibility

Browser compatibilityCompatibility data is available on the canonical MDN page.