Document

Zusammenfassung

Jede im Browswer geladene Webseite hat ihr eigenen document Objekt.  Das Document Interface dient als Einstiegspunkt in den Inhalt der Webseite (der DOM tree, beinhaltet Elemente wie <body> und <table>) und unterstützt Funktionen, die global für das Dokument gelten(z.B. das Aufrufen der URL der Seite und das Erstellen neuer Elemente im Dokument).

Ein Dokument-Objekt kann von verschiedenen APIs bezogen werden:

Depending on the kind of the document (e.g. HTML or XML), different APIs may be available on the document object.

  • All document objects implement the Document interface (and hence the Node and EventTarget interfaces). Thus, the "core" properties and methods documented on this page are available for all kinds of documents.
  • In contemporary browsers, some documents (e.g. those served with the text/html content type) also implement the HTMLDocument (en-US) interface.
  • In heutigen Browsern, SVG Dokumente implementieren das SVGDocument Interface.

In der Zukunft, alle diese Interfaces werden zusammengefasst zum Document Interface.

Eigenschaften

Hinweis: Das Document Interface erbt auch vom Node und EventTarget Interface.

Document.all (en-US) This deprecated API should no longer be used, but will probably still work. This API has not been standardized.
Provides access to all elements with an id. This is a legacy non-standard interface, you should use the Document.getElementById() method instead.
Document.async (en-US) This deprecated API should no longer be used, but will probably still work.
Used with document.load to indicate an asynchronous request.
Document.characterSet (en-US) This is an experimental API that should not be used in production code.
Returns the character set being used by the document.
Document.compatMode (en-US) This is an experimental API that should not be used in production code.
Indicates whether the document is rendered in Quirks or Strict mode.
Document.contentType (en-US) This is an experimental API that should not be used in production code.
Returns the Content-Type from the MIME Header of the current document.
Document.doctype (en-US)
Returns the Document Type Definition (DTD) of the current document.
Document.documentElement
Returns the Element that is a direct child of the document. For HTML documents, this is normally the HTML element.
Document.documentURI (en-US)
Returns the document URL.
Document.domConfig (en-US) This deprecated API should no longer be used, but will probably still work.
Should return a DOMConfiguration object.
Document.implementation (en-US)
Returns the DOM implementation associated with the current document.
Document.inputEncoding (en-US) This deprecated API should no longer be used, but will probably still work.
Returns the encoding used when the document was parsed.
Document.lastStyleSheetSet (en-US)
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 (en-US).
Document.mozSyntheticDocument (en-US) This API has not been standardized.
true if this document is synthetic, such as a standalone image, video, audio file, or the like.
Document.mozFullScreen (en-US) This API has not been standardized.
true when the document is in full-screen mode.
Document.mozFullScreenElement (en-US) This API has not been standardized.
The element that's currently in full screen mode for this document.
Document.mozFullScreenEnabled (en-US) This API has not been standardized.
true if calling element.mozRequestFullscreen() (en-US) would succeed in the curent document.
Document.pointerLockElement (en-US) This is an experimental API that should not be used in production code.
Returns the element set as the target for mouse events while the pointer is locked. null if lock is pending, pointer is unlocked, or if the target is in another document.
Document.preferredStyleSheetSet (en-US)
Returns the preferred style sheet set as specified by the page author.
Document.selectedStyleSheetSet (en-US)
Returns which style sheet set is currently in use.
Document.styleSheets (en-US)
Returns a list of the style sheet objects on the current document.
Document.styleSheetSets (en-US)
Returns a list of the style sheet sets available on the document.
Document.xmlEncoding (en-US) This deprecated API should no longer be used, but will probably still work.
Returns the encoding as determined by the XML declaration.
Document.xmlStandalone Veraltet seit Gecko 10.0
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.
Document.xmlVersion (en-US) Veraltet seit Gecko 10.0
Returns the version number as specified in the XML declaration or "1.0" if the declaration is absent.

The Document interface is extended with the ParentNode (en-US) interface:

ParentNode.childElementCount Read only
Returns the number of children of this ParentNode which are elements.
ParentNode.children Read only
Returns a live HTMLCollection containing all of the Element objects that are children of this ParentNode, omitting all of its non-element nodes.
ParentNode.firstElementChild Read only
Returns the first node which is both a child of this ParentNode and is also an Element, or null if there is none.
ParentNode.lastElementChild Read only
Returns the last node which is both a child of this ParentNode and is an Element, or null if there is none.

Erweiterung für HTML Dokumente

Das Document Interface für HTML Dokumente erbt vom HTMLDocument (en-US) Interface oder, ist seit HTML5 erweitert für solche Dokumente:

Document.activeElement (en-US)
Returns the currently focused element.
Document.alinkColor This deprecated API should no longer be used, but will probably still work.
Returns or sets the color of active links in the document body.
Document.anchors (en-US)
Returns a list of all of the anchors in the document.
Document.applets (en-US) This deprecated API should no longer be used, but will probably still work.
Returns an ordered list of the applets within a document.
Document.bgColor (en-US) This deprecated API should no longer be used, but will probably still work.
Gets/sets the background color of the current document.
Document.body
Returns the <body> element of the current document.
Document.cookie (en-US)
Returns a semicolon-separated list of the cookies for that document or sets a single cookie.
Document.defaultView (en-US)
Returns a reference to the window object.
Document.designMode (en-US)
Gets/sets the ability to edit the whole document.
Document.dir
Gets/sets directionality (rtl/ltr) of the document.
Document.domain (en-US)
Returns the domain of the current document.
Document.embeds (en-US)
Returns a list of the embedded <embed> elements within the current document.
Document.fgColor (en-US) This deprecated API should no longer be used, but will probably still work.
Gets/sets the foreground color, or text color, of the current document.
Document.forms (en-US)
Returns a list of the <form> (en-US) elements within the current document.
Document.head
Returns the <head> element of the current document.
Document.height (en-US) This API has not been standardized. Dies ist eine veraltete API und es wird nicht mehr garantiert, dass sie funktioniert.
Gets/sets the height of the current document.
Document.images (en-US)
Returns a list of the images in the current document.
Document.lastModified (en-US)
Returns the date on which the document was last modified.
Document.linkColor (en-US) This deprecated API should no longer be used, but will probably still work.
Gets/sets the color of hyperlinks in the document.
Document.links (en-US)
Returns a list of all the hyperlinks in the document.
Document.location (en-US)
Returns the URI of the current document.
Document.plugins (en-US)
Returns a list of the available plugins.
Document.readyState
Returns loading status of the document.
Document.referrer
Returns the URI of the page that linked to this page.
Document.scripts (en-US)
Returns all the <script> (en-US) elements on the document.
Document.title
Returns the title of the current document.
Document.URL
Returns a string containing the URL of the current document.
Document.vlinkColor (en-US) This deprecated API should no longer be used, but will probably still work.
Gets/sets the color of visited hyperlinks.
Document.width This API has not been standardized. Dies ist eine veraltete API und es wird nicht mehr garantiert, dass sie funktioniert.
Returns the width of the current document.

Event handlers

Document.onpointerlockchange This is an experimental API that should not be used in production code.
Returns the event handling code for the pointerlockchange (en-US) event.
Document.onpointerlockerror This is an experimental API that should not be used in production code.
Returns the event handling code for the pointerlockerror (en-US) event.
Document.onreadystatechange
Returns the event handling code for the readystatechange event.

Methoden

Hinweis: Das Document Interface erbt auch vom Node und EventTarget Interface.

Document.adoptNode(Node node)
Adopt node from an external document.
Document.captureEvents(String eventName) This deprecated API should no longer be used, but will probably still work.
See window.captureEvents (en-US).
Document.caretPositionFromPoint(Number x, Number y) (en-US)
Gets a CaretPosition (en-US) based on two coordinates.
Document.createAttribute(String name)
Creates a new Attr (en-US) object and returns it.
Document.createAttributeNS(String namespace, String name)
Creates a new attribute node in a given namespace and returns it.
Document.createCDATASection(String data) (en-US)
Creates a new CDATA node and returns it.
Document.createComment(String comment) (en-US)
Creates a new comment node and returns it.
Document.createDocumentFragment()
Creates a new document fragment.
Document.createElement(String name)
Creates a new element with the given tag name.
Document.createElementNS(String namespace, String name)
Creates a new element with the given tag name and namespace URI.
Document.createEntityReference(String name) (en-US) Dies ist eine veraltete API und es wird nicht mehr garantiert, dass sie funktioniert.
Creates a new entity reference object and returns it.
Document.createEvent(String interface) (en-US)
Creates an event object.
Document.createNodeIterator(Node root[, Number whatToShow[, NodeFilter filter]]) (en-US)
Creates a NodeIterator (en-US) object.
Document.createProcessingInstruction(String target, String data) (en-US)
Creates a new ProcessingInstruction (en-US) object.
Document.createRange() (en-US)
Creates a Range object.
Document.createTextNode(String text)
Creates a text node.
Document.createTreeWalker(Node root[, Number whatToShow[, NodeFilter filter]])
Creates a TreeWalker (en-US) object.
Document.elementFromPoint(Number x, Number y) (en-US)
Returns the element visible at the specified coordinates.
Document.enableStyleSheetsForSet(String name) (en-US)
Enables the style sheets for the specified style sheet set.
Document.exitPointerLock() (en-US) This is an experimental API that should not be used in production code.
Release the pointer lock.
Document.getElementsByClassName(String className)
Returns a list of elements with the given class name.
Document.getElementsByTagName(String tagName) (en-US)
Returns a list of elements with the given tag name.
Document.getElementsByTagNameNS(String namespace, String tagName) (en-US)
Returns a list of elements with the given tag name and namespace.
Document.importNode(Node node, Boolean deep)
Returns a clone of a node from an external document.
document.mozSetImageElement (en-US) This API has not been standardized.
Allows you to change the element being used as the background image for a specified element ID.
Document.normalizeDocument() Dies ist eine veraltete API und es wird nicht mehr garantiert, dass sie funktioniert.
Replaces entities, normalizes text nodes, etc.
Document.releaseCapture() (en-US) This API has not been standardized.
Releases the current mouse capture if it's on an element in this document.
Document.releaseEvents This API has not been standardized. This deprecated API should no longer be used, but will probably still work.
See window.releaseEvents (en-US).
document.routeEvent This API has not been standardized. Veraltet seit Gecko 24
See window.routeEvent (en-US).

The Document interface is extended with the ParentNode (en-US) interface:

Document.getElementById(String id)
Returns an object reference to the identified element.
Document.querySelector(String selector)
Returns the first Element node within the document, in document order, that matches the specified selectors.
Document.querySelectorAll(String selector)
Returns a list of all the Element nodes within the document that match the specified selectors.

The Document interface is extended with the XPathEvaluator (en-US) interface:

Document.createExpression(String expression, XPathNSResolver resolver) (en-US)
Compiles an XPathExpression which can then be used for (repeated) evaluations.
Document.createNSResolver(Node resolver) (en-US)
Creates an XPathNSResolver (en-US) object.
Document.evaluate(String expression, Node contextNode, XPathNSResolver resolver, Number type, Object result) (en-US)
Evaluates an XPath expression.

Erweiterung für HTML Dokumente

Das Document Interface für HTML Dokumente erbt vom HTMLDocument (en-US) Interface oder ist seit HTML5 erweitert für solche Dokumente:

Document.clear() (en-US) This API has not been standardized. This deprecated API should no longer be used, but will probably still work.
In majority of modern browsers, including recent versions of Firefox and Internet Explorer, this method does nothing.
Document.close() (en-US)
Closes a document stream for writing.
Document.execCommand(String command[, Boolean showUI[, String value]]) (en-US)
On an editable document, executes a formating command.
Document.getElementsByName(String name) (en-US)
Returns a list of elements with the given name.
Document.getSelection() (en-US)
Returns a Selection (en-US) object related to text selected in the document.
Document.hasFocus() (en-US)
Returns true if the focus is currently located anywhere inside the specified document.
Document.open() (en-US)
Opens a document stream for writing.
Document.queryCommandEnabled(String command) (en-US)
Returns true if the formating command can be executed on the current range.
Document.queryCommandIndeterm(String command)
Returns true if the formating command is in an indeterminate state on the current range.
Document.queryCommandState(String command) (en-US)
Returns true if the formating command has been executed on the current range.
Document.queryCommandSupported(String command) (en-US)
Returns true if the formating command is supported on the current range.
Document.queryCommandValue(String command)
Returns the current value of the current range for a formatting command.
Document.registerElement(String tagname[, Object options])
Registers a new custom element in the browser and returns a constructor for the new element.
Document.write(String text)
Writes text in a document.
Document.writeln(String text)
Writes a line of text in a document.

Spezifikationen

Spezifikation Status Kommentar
Document Object Model (DOM) Level 1 Specification
Die Definition von 'Document' in dieser Spezifikation.
Veraltet Initial definition for the interface
Document Object Model (DOM) Level 2 Core Specification
Die Definition von 'Document' in dieser Spezifikation.
Veraltet Supersede DOM 1
Document Object Model (DOM) Level 3 Core Specification
Die Definition von 'Document' in dieser Spezifikation.
Veraltet Supersede DOM 2
DOM
Die Definition von 'Document' in dieser Spezifikation.
Lebender Standard Intend to supersede DOM 3
HTML Living Standard
Die Definition von 'Document' in dieser Spezifikation.
Lebender Standard Turn the HTMLDocument (en-US) interface into a Document extension.
Document Object Model (DOM) Level 3 XPath Specification
Die Definition von 'XPathEvaluator' in dieser Spezifikation.
Empfehlung Define the XPathEvaluator (en-US) interface which extend Document.
HTML Editing APIs
Die Definition von 'Document' in dieser Spezifikation.
Bearbeiterentwurf Extend the Document interface
CSS Object Model (CSSOM) View Module
Die Definition von 'Document' in dieser Spezifikation.
Arbeitsentwurf Extend the Document interface
CSS Object Model (CSSOM)
Die Definition von 'Document' in dieser Spezifikation.
Arbeitsentwurf Extend the Document interface
Pointer Lock
Die Definition von 'Document' in dieser Spezifikation.
Anwärter Empfehlung Extend the Document interface

Browser Kompatibilität

Firefox Hinweise

Mozilla defines a set of non-standard properties made only for XUL content:

document.currentScript (en-US)
Returns the <script> (en-US) element that is currently executing.
document.documentURIObject (en-US)
(Mozilla add-ons only!) Returns the nsIURI object representing the URI of the document. This property only has special meaning in privileged JavaScript code (with UniversalXPConnect privileges).
document.popupNode (en-US)
Returns the node upon which a popup was invoked.
document.tooltipNode (en-US)
Returns the node which is the target of the current tooltip.

Mozilla also define some non-standard methods:

Document.execCommandShowHelp Veraltet seit Gecko 14.0
This method never did anything and always threw an exception, so it was removed in Gecko 14.0 (Firefox 14.0 / Thunderbird 14.0 / SeaMonkey 2.11).
Document.getBoxObjectFor (en-US) Dies ist eine veraltete API und es wird nicht mehr garantiert, dass sie funktioniert.
Use the Element.getBoundingClientRect() method instead.
Document.loadOverlay
Loads a XUL overlay dynamically. This only works in XUL documents.
document.queryCommandText Veraltet seit Gecko 14.0
This method never did anything but throw an exception, and was removed in Gecko 14.0 (Firefox 14.0 / Thunderbird 14.0 / SeaMonkey 2.11).

Internet Explorer Hinweise

Microsoft defines some non-standard properties:

document.fileSize* This API has not been standardized. Dies ist eine veraltete API und es wird nicht mehr garantiert, dass sie funktioniert.
Returns size in bytes of the document. Starting with Internet Explorer 11, that property is no longer supported. See MSDN.
Internet Explorer does not support all methods from the Node interface in the Document interface:
document.contains
As a work-around, document.body.contains() can be used.