Home to Document Object Model | Up to Core
Prev Text | Next DocumentFragment


Document
Object (inherits from Node).

This object represents and contains an entire document. It provides the factory methods required to create all types of child Node (see the create* methods). Nodes created this way have their ownerDocument set to the document used to create them.

A document node may have the following child nodes:

doctype
Attribute (read only, DocumentType)

See DocumentType and DOMImplementation for details. Returns the DocumentType node of the document.

documentElement
Attribute (read only, Element)

Returns the root Element of the document. For example, in a valid XHTML document, this will be the element with a tagName of "HTML" (see also getElementsByTagName* methods).

implementation
Attribute (read only, DOMImplementation)

See DOMImplementation for details. Returns the DOMImplementation in use by the document.

createAttribute
Method
ECMAScript binding: createAttribute(name) (returns Attr; name is a string; can raise DOMException)

Creates an Attr node with the nodeName attribute set to name and localName, prefix and namespaceURI set to null. The value is the empty string. The attribute can be applied to an element using that object's setAttributeNode method. See also createAttributeNS.

This method allows values to be set on attributes on elements. Take this HTML markup:

<UL type="compact">
</UL>
The "UL" element has an attribute called "type" with a value of "compact". This could be coded
var ULele = document.createElement("UL");
var typeAttr = document.createAttribute("type");
typeAttr.value = "compact";
ULele.setAttributeNode(typeAttr);

The exceptions thrown are:

INVALID_CHARACTER_ERR
name contains an illegal character.

createAttributeNS
Method (only present if feature "XML" is present at version "2.0")
ECMAScript binding: createAttributeNS(namespaceURI, qualifiedName) (returns Attr; namespaceURI and qualifiedName are strings; can raise DOMException)

Creates an Attr node with the nodeName and name set to qualifiedName, namespaceURI set to namespaceURI and prefix and localName extracted from qualifiedName. The value is the empty string. The attribute can be applied to an element using that object's setAttributeNode method. See also createAttribute.

This method allows values to be set on attributes on elements. Take this XML markup:

<nsn:myList xmlns:nsn="http://my.website.com/nsn" nsn:type="compact"/>
The "nsn:myList" element has an attribute called "nsn:type" (URI="http://my.website.com/nsn") with a value of "compact". This could be coded
var myListEle = document.createElement("myList");
var typeAttr = document.createAttributeNS("http://my.website.com/nsn", "nsn:type");
typeAttr.value = "compact";
myListEle.setAttributeNode(typeAttr);

The exceptions thrown are:

INVALID_CHARACTER_ERR
qualifiedName contains an illegal character.
NAMESPACE_ERR
qualfiedName is otherwise malformed (e.g. has a prefix but the namespaceURI is null).

createCDATASection (only present if feature "XML" is present at version "2.0")
Method
ECMAScript binding: createCDATASection(data) (returns CDATASection; data is a string; can raise DOMException)

Creates a CDATASection node with the value set to data. Characters within the node are not parsed for markup, so do not need to be escaped. CDATA Sections are used in markup like this:

<script><![CDATA[if (a < b) foo()]]></script>

The "<" in the script will not be parsed by the markup processor. To create the CDATA section, the following could be coded:

var CDATAnode = document.createCDATASection("if (a < b) foo()");

The exceptions thrown are:

NOT_SUPPORTED_ERR
This document type does not support CDATASection nodes (e.g. HTML documents).

createComment
Method
ECMAScript binding: createComment(data) (returns Comment; data is a string)

Creates a Comment node with the value set to data. Comment nodes represent the

<!-- Comment text -->
markup. The data in the example would be " Comment text " (with the surrounding blanks).

createDocumentFragment
Method
ECMAScript binding: createDocumentFragment() (returns DocumentFragment)

See DocumentFragment for use and examples.

createElement
Method
ECMAScript binding: createElement(tagname) (returns Element; tagname is astrings; can raise DOMException)

Creates an Element node with the tagName set to tagname. namespaceURI, prefix and localName are null. See also createElementNS.

For example, to create an HTML unordered list element, one would code

var ULele = document.createElement("UL");
The equivalent markup, obviously, would be
<UL>
</UL>
or, in XHTML or XML
<UL/>

The exceptions thrown are:

INVALID_CHARACTER_ERR
tagname contains an illegal character.

createElementNS
Method (only present if feature "XML" is present at version "2.0")
ECMAScript binding: createElementNS(namespaceURI, qualifiedName) (returns Element; namespaceURI and qualifiedName are strings; can raise DOMException)

Creates an Element node with the nodeName and tagName set to qualifiedName, namespaceURI set to namespaceURI and prefix and localName extracted from qualifiedName. See also createElement.

For example,

<nsn:myList xmlns:nsn="http://my.website.com/nsn"/>
could be coded as
var myListEle = document.createElementNS("http://my.website.com/nsn", "nsn:myList");

The exceptions thrown are:

INVALID_CHARACTER_ERR
qualifiedName contains an illegal character.
NAMESPACE_ERR
qualfiedName is otherwise malformed (e.g. has a prefix but the namespaceURI is null).
WRONG_DOCUMENT_ERR
doctype is referenced in another Document object or was created under a different DOMImplementation.

createEntityReference
Method
ECMAScript binding: createEntityReference(name) (returns EntityReference; name is a string; can raise DOMException)

See EntityReference and Entity for use and examples.

The exceptions thrown are:

INVALID_CHARACTER_ERR
name contains an illegal character.
NOT_SUPPORTED_ERR
This document type does not support Entity references (e.g. HTML documents).

createProcessingInstruction
Method
ECMAScript binding: createProcessingInstruction(target, data) (returns ProcessingInstruction; target and data are a strings; can raise DOMException)

See ProcessingInstruction for use and examples.

The exceptions thrown are:

INVALID_CHARACTER_ERR
target contains an illegal character.
NOT_SUPPORTED_ERR
This document type does not support processing instructions (e.g. HTML documents).

createTextNode
Method
ECMAScript binding: createTextNode(data) (returns Text; data is a string)

See Text for use and examples. Text nodes hold text for Elements and Attrs.

getElementById
Method (only present if feature "Core" is present at version "2.0")
ECMAScript binding: getElementById(elementId) (returns Element; elementId is a string)

Returns the Element with an Attr named "ID" whose value is elementID, if present in the document. Otherwise, null.

Note that "ID" attributes should be unique within a document. Should this rule be violated, the behaviour of this method is undefined.

Note, also, that it's actually up to the implementation to decide what attribute to use as the ID. It's not necessarily one named "ID".

getElementsByTagName
Method
ECMAScript binding: getElementsByTagName(tagname) (returns NodeList; tagname is a string)

Returns a NodeList containing Elements with the given tagname (or an empty list if none is present). "*" matches everything.

getElementsByTagNameNS
Method (only present if feature "Core" is present at version "2.0")
ECMAScript binding: getElementsByTagNameNS(namespaceURI, localName) (returns NodeList; namespaceURI and localName are strings)

Returns a NodeList containing Elements with the given namespaceURI and localName (or an empty list if none is present). "*" matches everything and can be specified for either or both parameters.

importNode
Method (only present if feature "Core" is present at version "2.0")
ECMAScript binding: importNode(importedNode, deep) (returns Node; importedNode is a Node, deep is Boolean; can raise DOMException)

The standard is actually crystal clear on this method. It says:

Imports a node from another document to this document. The returned node has no parent; (parentNode is null). The source node is not altered or removed from the original document; this method creates a new copy of the source node.

For all nodes, importing a node creates a node object owned by the importing document, with attribute values identical to the source node's nodeName and nodeType, plus the attributes related to namespaces (prefix, localName, and namespaceURI). As in the cloneNode operation on a Node, the source node is not altered.

Additional information is copied as appropriate to the nodeType, attempting to mirror the behavior expected if a fragment of XML or HTML source was copied from one document to another, recognizing that the two documents may have different DTDs in the XML case.

importedNode is the source node referred to above. deep is true to request recursion.

The exceptions thrown are:

NOT_SUPPORTED_ERR
This document type does not support the node type being imported.