Object: Document
Document properties
Property name |
Type |
Description |
Status |
Availability |
|---|---|---|---|---|
defaultView |
AbstractView |
Returns the default view of the document. |
read only |
4.6 or later |
doctype |
Returns the document type declaration associated with the document. |
read only |
4.6 or later |
|
documentElement |
Returns the root element in a document. |
read only |
4.6 or later |
|
implementation |
Returns the DOMImplementation object that handles this document. |
read only |
4.6 or later |
|
parentWindow |
Returns a reference to the parent window. If no parent window exists, this property points to the current active window. |
read only |
4.6 or later |
|
styleSheets |
Returns an array of all the style sheets in the document. |
read only |
4.6 or later |
Document methods
Method name |
Description |
Availability |
|---|---|---|
Adopts a node and its children from an external document to the current document. |
4.7.1 or later |
|
Creates a new Attr object with the given name. |
4.6 or later |
|
Creates a new Attr object with the given qualified name and namespace URI. |
4.6 or later |
|
Creates a new CDATASection object, which represents a block of uninterpreted content. |
4.6 or later |
|
Creates a new Comment object with the given text as content. |
4.6 or later |
|
Creates an empty DocumentFragment object. |
4.6 or later |
|
Creates a new Element object of the specified type. |
4.6 or later |
|
Creates a new Element object of the given qualified name and namespace URI. |
4.6 or later |
|
Creates a new EntityReference object with the given name. |
4.6 or later |
|
Creates a new Event object of the given type. |
4.6 or later |
|
Creates a new NodeIterator object, which you can use to iterate through a set of Document nodes rooted at the given node. |
4.6 or later |
|
Creates a new ProcessingInstruction object with the given name and data. |
4.6 or later |
|
Creates a new Range object. |
4.7.1 or later |
|
Creates a new Text node containing the given text. |
4.6 or later |
|
Creates a TreeWalker object, which you can use to traverse the specified document subtree. |
4.6 or later |
|
Returns the element node with the given ID value. |
4.6 or later |
|
Returns a NodeList object that contains an array of a document’s descendent element nodes with the specified tag name. |
4.6 or later |
|
Returns a NodeList object that contains an array of a document’s descendent element nodes with the given local name and namespace URI. |
4.6 or later |
|
Retrieves the override CSSStyleDeclaration object for the given element. |
4.6 or later |
|
Imports a node from a different document. |
4.6 or later |
Method: Document.adoptNode()
The adoptNode() method moves a node and its child nodes from another document to the current document. Unlike the Document.importNode() method, which imports a copy of the source node, the adoptNode() method removes a node from the one document and adds it to another.
If the adopted node has a parent node, this method first deletes the adopted node from the child list of the parent node, then changes the ownerDocument property of the adopted node to reflect the current document.
The adopted node must be inserted into the desired location within the current document tree by using the Node.appendChild() method or a similar method.
Parameters
Exceptions
Exception |
Description |
|---|---|
NO_MODIFICATION_ALLOWED_ERR |
Thrown if sourceNode is read-only. |
NOT_SUPPORTED_ERR |
Thrown if sourceNode is of a type that cannot be adopted, such as a Document or DocumentType node. |
Method: Document.createAttribute()
The createAttribute() method creates a new Attr node with the given name. Once created, the attribute instance can be bound to an element using Element.setAttributeNode().
This method only populates the Node.nodeName property for the created attribute and sets the attribute’s value to null.
To create an attribute with a qualified name and namespace URI, use Document.createAttributeNS().
Return values
Returns the new attribute node with the Node.nodeName property set to name and the Node.nodeValue property set to an empty string.
Method: Document.createAttributeNS()
The createAttributeNS() method creates a new Attr object with the given qualified name and namespace URI. Once created, the attribute instance can be bound to an element using Element.setAttributeNode().
Return values
Returns the new Attr object with the following properties set:
Property |
Description |
|---|---|
The qualified name. |
|
The namespace URI for the attribute. |
|
The prefix, extracted from the qualified name of the new attribute. If there is no prefix, this property is set to null. |
|
The local name for the attribute, extracted from the qualified name. |
|
An empty string. |
|
Equivalent to Node.nodeName. |
Method: Document.createCDATASection()
The createCDATASection() method creates a new CDATASection object, which represents a block of uninterpreted content.
HTML documents do not support CDATASection objects.
Method: Document.createComment()
The createComment() method creates a new Comment object with the given text as content.
Method: Document.createDocumentFragment()
The createDocumentFragment() method creates an empty DocumentFragment object.
A DocumentFragment object can be used to hold and move a portion of a document—such as multiple parent nodes and their children—at once.
Method: Document.createElement()
The createElement() method creates a new Element object of the specified type. Attributes can be set directly on the returned object.
If the Element type returned has any attributes with default values, those attributes will be created and attached to the element with their default values.
Return values
Returns the new Element object, with the Node.nodeName property set to tagName, and zero or more Attr objects attached, representing the known attributes that have default values.
Method: Document.createElementNS()
The createElementNS() method creates a new Element object of the given qualified name and namespace URI. Attributes can be set directly on the returned object.
Return values
Returns the new Element object with the following properties set:
Method: Document.createEntityReference()
The createEntityReference() method creates a new EntityReference object with the given name.
If the referenced entity is known, then that object’s list of child nodes is made the same as the corresponding Entity node.
Method: Document.createEvent()
The createEvent() method creates a new Event object of the given type.
If the event is to be dispatched via Node.dispatchEvent(), then the appropriate initialization method must be called after the event has been created, and before it is dispatched, in order to initialize the event’s values.
This method is used to create events when it is inconvenient or unnecessary for the user to create an event themselves.
Exceptions
Exception |
Description |
|---|---|
NOT_SUPPORTED_ERR |
This error is thrown if the implementation does not support the specified Event interface. |
Method: Document.createNodeIterator()
The createNodeIterator() method creates a new NodeIterator object, which you can use to iterate through a set of Document nodes rooted at the given node.
Parameters
Method: Document.createProcessingInstruction()
The createProcessingInstruction() method creates a new ProcessingInstruction object with the given name and data.
Method: Document.createRange()
The createRange() method creates a new empty Range object.
The Range object can be used only to select content that is associated with the current document, or with DocumentFragment or Attr objects for which this document is the owner.
Method: Document.createTextNode()
The createTextNode() method creates a new Text node containing the given text.
You can create new text nodes to add text to an existing node, then use Node.normalize() to merge them into a single node later on.
Method: Document.createTreeWalker()
The createTreeWalker() method creates a TreeWalker object, which you can use to traverse the specified document subtree.
Parameters
Parameter |
Type |
Description |
|---|---|---|
root |
The node that will serve as the root for this TreeWalker object. The TreeWalker.currentNode property is set to the value of root. |
|
whatToShow |
unsigned long |
The types of nodes to show in the tree view. The value may be one or more of:
Multiple values may be specified using a bitwise OR. Note that because attributes, entities, and notations are not the child of any other node, they never appear in the TreeWalker object’s logical view unless root is defined as a Node of that type. If the respective object is not defined as the root, then SHOW_ATTRIBUTE, SHOW_ENTITY, and SHOW_NOTATION have no meaning. |
filter |
The filter to use with this TreeWalker object. This parameter may be null. |
|
entityReferenceExpansion |
boolean |
When true, this method specifies that entity reference nodes should be expanded. |
Method: Document.getElementById()
The getElementById() method returns the Element node with the given ID value.
Return values
Returns the Element object with the given ID value, or null if no element with that ID value exists.
Code sample
The following example appends a text node as a child of a new Paragraph element, then appends the Paragraph node as a child of the node divb.
Function appendTextNode() {
var divb = document.getElementById(‘b’);
var str = document.getElementById(‘a’).value;
var pnode = document.createElement(‘p’);
var tnode = document.createTextNode(str);
pnode.appendChild(tnode);
divb.appendChild(pnode);
Method: Document.getElementsByTagName()
The getElementsByTagName() method returns a NodeList object that contains an array of a document’s descendent Element nodes with the specified tag name.
Method: Document.getElementsByTagNameNS()
The getElementsByTagNameNS() method returns a NodeList object that contains an array of a document’s descendent Element nodes with the given local name and namespace URI.
Method: Document.getOverrideStyle()
The getOverrideStyle() method retrieves the override CSSStyleDeclaration object for the given element.
Override styles take precedence over authored styles, although authored styles which contain the “!important” declaration maintain precedence and cannot be overridden using this method.
Using this method, you can override an explicitly linked style sheet without changing the authored style sheet itself.
Method: Document.importNode()
The importNode() method imports a node from a different document. This method is similar to Node.cloneNode(), except that it functions across documents.
The returned node has no parent, and the source node is not altered or removed from the original document; this method creates a new copy of the source node.
Importing a node creates a new Node object owned by the document the node is imported into, with values identical to the Node.nodeName and Node.nodeType, plus the attributes related to namespaces. Additional information is copied as appropriate to the node type.
Parameters
Parameter |
Type |
Description |
|---|---|---|
importedNode |
The node to import into the current document. |
|
deep |
boolean |
When true, this method imports the given node and the subtree. When false, this method only imports the given node; it does not include the subtree. This parameter has no effect when the given node is an Attr, EntityReference, or Notation node. |