This chapter contains a reference to all of the DOM Level 2 Core language bindings for ECMAScript (that is, Netscape JavaScript and MS JScript). The DOM Level 2 Core API provides the programmer with tools to create valid and well-formed XML documents. In addition, functionality exists in the API to navigate XML documents as well as modify the underlying structure. For those familiar with the DOM Java language bindings, you will notice that the ECMAScript bindings bear a striking resemblance to the Java DOM API.
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
Core DOM object.
Attr
represents an attribute in an Element
. Attr
inherits all methods and properties from Node
. DOM considers Attrs
not to be part of the document tree. Therefore,parentNode, previousSibling, and nextSibling are all null. Table 10.1 lists all the properties of the Attr
object.
An Attr
is created by using the CreateAttribute()
method of the Document
object.
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
attrObj.name
The name
property of the Attr
object returns the name of this attribute.
Listing 10.1 iterates through a list of attributes and writes out each attribute’s name.
<html>
<script language="JScript">
<!--
var attrList = nodeObj.attributes;
var i = 0;
while(i < attrList.length) {
var attrObj = attrList.item(i);
writeAttrName(attrObj.name);
i++;
}
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
attrObj.specified
The read-only property specified
of the Attr
object returns true if this attribute’s value was explicitly assigned in the original document.
Listing 10.2 iterates through a list of attributes checking to see which ones were specified in the original XML document.
<html>
<script language="JScript">
<!--
var attrList = nodeObj.attributes;
var i = 0;
while(i < attrList.length) {
var attrObj = attrList.item(i);
if(attrObj.specified)
handleSpecifiedAttr(attrObj);
i++;
}
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
attrObj.Value
The value
property of the Attr
object returns the value for this attribute as a string. A DOMException
object can be raised with value NO_MODIFICATION_ALLOWED_ERR if this node is read-only.
Listing 10.3 checks the value of a size attribute.
<html>
<script language="JScript">
<!--
var sizeValue = sizeAttrObj.value;
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
attrObj.ownerElement
The ownerElement
property of the Attr
object returns the Element
node that this attribute is attached to.
Listing 10.4 retrieves the owner element for an attribute and then removes the attribute from the element.
<html>
<script language="JScript">
<!--
var elementObj = attrObj.ownerElement;
elementObj.removeAttribute(attrObj);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
Core DOM object.
The CDATASection
object inherits all methods and properties from the Text
object and the CharacterData
object. The CDATASection
encapsulates XML CDATA sections that are used to escape blocks of text that shouldn’t be interpreted as markup.
A CDATASection
object is instantiated using the CreateCDATASection()
method of the Document
object.
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
Core DOM object.
This object is inherited by other objects that need to access character data in DOM. CharacterData
inherits all properties and methods from Node in addition to those listed in Table 10.2.
CharacterData
objects are never directly instantiated. They exist through subclasses such as Text
and Comment
.
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
characterDataObj.data
The readonly property data
of the CharacterData
object returns the character data of this node.
Listing 10.5 retrieves string data from a Text
node and displays it.
<html>
<script language="JScript">
<!--
var textData = textObj.data;
write(textData);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
characterDataObj.length
The read-only property length
of the CharacterData
object returns the length of data in 16-bit units.
Listing 10.6 queries a Text
node for the length of its data field.
<html>
<script language="JScript">
<!--
var dataSize = textObj.length;
if(dataSize > bufferSize)
printError();
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
characterDataObj.substringData(offset, count)
The substringData()
method of the CharacterData
object returns a range of data from the node with the offset character at position offset and at length of count. A DOMException
object can be raised with value INDEX_SIZE_ERR if the specified offset is negative or greater than the Length, or with value NO_MODIFICATION_ALLOWED_ERR if this node is read-only.
Listing 10.7 creates a substring from the given Text
node’s data.
<html>
<script language="JScript">
<!--
textObj.data = "Test Text Data";
var subStringData = textObj.subStringData(0, 4);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
characterDataObj.appendData(arg)
The appendData()
method of the CharacterData
object appends the string arg to the end of Data. A DOMException
object can be raised with value NO_MODIFICATION_ ALLOWED_ERR if the node is read-only.
Listing 10.8 adds a string to the end of a Text node’s data property.
<html>
<script language="JScript">
<!--
textObj.data = "Test Text Data";
textObj.appendData("Some more data");
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
characterDataObj.insertData(offset, arg)
The insertData()
method of the CharacterData
object inserts a string arg at the specified position offset. A DOMException
object can be raised with value INDEX_SIZE_ERR if the specified offset is negative or greater than Length or with the value NO_MODIFICATION_ALLOWED_ERR if this node is read-only.
Listing 10.9 inserts a string into a Text
node’s Data
property.
<html>
<script language="JScript">
<!--
textObj.data = "Test Text Data";
textObj.insertData(4, "Some more data");
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
characterDataObj.deleteData(offset, count)
The deleteData()
method of the CharacterData
object removes a range of data from the node beginning at position offset and removing count characters. A DOMException
object can be raised with the value INDEX_SIZE_ERR if the specified offset is negative or exceeds Length
or with the value NO_MODIFICATION_ALLOWED_ERR raised if the node is read-only.
Listing 10.10 deletes a string from a Text
node’s Data
property.
<html>
<script language="JScript">
<!--
textObj.data = "Test Text Data";
textObj.deleteData(0, 5);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
characterDataObj.replaceData(offset, count, arg)
The replaceData()
method of the CharacterData
object replaces the characters of data contained in this node beginning at position offset and length count with the string arg.
Listing 10.11 illustrates replacing one string for another in a Text
node’s Data
property.
<html>
<script language="JScript">
<!--
textObj.data = "Test Text Data";
textObj.replaceData(0, 4, "foo ");
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
Core DOM object.
Comment
inherits all methods and properties from the CharacterData
object and represents the contents of an XML comment.
A Comment
is instantiated using the createComment()
method of the Document
object.
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
Core DOM object.
A Document
object is created using the CreateDocument()
method of the DOMImplementation
object. Table 10.3 lists all properties and methods of the Document
object.
Listing 10.12 uses a DOMImplementation
object to create a Document
object.
<html>
<script language="JScript">
<!--
var domImplObj = new DOMImplementation();
var documentObj = domImplObj.
createDocument("http://foobar.org/schema", "foo:bar", null);
//-->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
documentObj.createAttribute(name)
The createAttribute()
method of the Document
object creates and returns an Attr
object with the given name. A DOMException
object can be raised with value INVALID_CHARACTER_ERR
if the specified name contains an illegal character.
Listing 10.13 illustrates adding a new attribute to the root element of a previously created document object.
<html>
<script language="JScript">
<!--
var attrObj = documentObj.createAttribute("size");
var rootElement - documentObj.rootElement;
attrObj.value = 34;
rootElement.setAttributeNode(attrObj);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
documentObj.createAttributeNS(namespaceURI, qualifiedName)
The createAttributeNS()
method of the Document
object creates and returns an Attr
of type qualifiedName residing in the namespace nameSpaceURI. The Attr
returned has its nodeName set to qualifiedName, its nameSpaceURI attribute set to nameSpaceURI, its prefix attribute set to a prefix as extracted from qualifiedName or null if there is no prefix, its localName attribute set to the local name as extracted from qualifiedName, its name attribute set to qualifiedName, and its nodeValue
attribute set to empty string.
It should be noted that implementations aren’t required to implement this method of Document
. A DOMException
object can be raised with the value INVALID_ CHARACTER_ERR if qualifiedName contains an illegal character or a value of NAME-SPACE_ERR if qualifiedName is malformed, the qualifiedName has a prefix and namespaceURI
is Null, or if qualifiedName has a prefix that is xml
and the namespaceURI
is other than http://www.w3.org/XML/1998/namespace
.
Listing 10.14 illustrates creating an attribute using namespace references.
<html>
<script language="JScript">
<!--
var newAttribute = documentObj.createAttributeNS
("http://foo.com/namespace", "foo:quantity");
newAttribute.Value = 10;
myLineItemElement.SetAttributeNode(newAttribute);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
documentObj.createCDATASection(data)
The createCDATASection()
method of the Document
object creates and returns a new instance of CDATASection
with its value set to data. A DOMException
object with the value NOT_SUPPORTED_ERR can be raised if this document is an HTML document.
Listing 10.15 illustrates how to create and add a CDATA section to a previously created document.
<html>
<script language="JScript">
<!--
var cdataSectionObj = documentObj.createCDATASection("foo");
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
documentObj.createComment(data)
The createComment()
method of the Document
object creates and returns a new Comment
node using the DOMString
data.
Listing 10.16 uses a previously created Document
object to create and add a new Comment
to the current XML document.
<html>
<script language="JScript">
<!--
var commentObj = documentObj.createComment
("Here is an example comment.");
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
documentObj.createDocumentFragment()
The createDocumentFragment()
method of the Document
object creates an empty DocumentFragment
object.
Listing 10.17 uses a Document
object to create a new DocumentFragment
.
<html>
<script language="JScript">
<!--
var documentFragmentObj = documentObj.createDocumentFragment();
var nodeType = documentFragmentObj.NodeType;
if(nodeType != Node.DOCUMENT_FRAGMENT_NODE)
printError();
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
documentObj.createElement(tagName)
The createElement()
method of the Document
object creates a new element of the type specified by tagName. This method returns a new instance of Element
with its nodeName attribute set to tagName and its other attributes set to Null. A DOMException
object can be thrown with value INVALID_CHARACTER_ERR if the specified tagName contains an illegal character.
Listing 10.18 uses a Document
object to create a new element.
<html>
<script language="JScript">
<!--
var elementObj = documentObj.createElement("foo");
var hasChildren = elementObj.hasChildren();
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
documentObj.CreateElementNS(nameSpaceURI, qualifiedName)
The createElementNS()
method of the Document
object creates and returns an Element
of type qualifiedName residing in the namespace nameSpaceURI. The Element
returned has its nodeName set to qualifiedName, its nameSpaceURI attribute set to nameSpaceURI, its prefix attribute set to the prefix as extracted from qualifiedName, its localName attribute set to the localName as extracted from qualifiedName, and its tagName attribute set to qualifiedName. It should be noted that implementations aren’t required to implement this method of Document
. A DOMException
object can be raised with the value INVALID_CHARACTER_ERR if qualifiedName contains an illegal character or a value of NAMESPACE_ERR if qualifiedName is malformed, the qualifiedName has a prefix and namespaceURI is null, or if qualifiedName has a prefix that is xml
and the namespaceURI
is other than http://www.w3.org/XML/1998/namespace
.
Listing 10.19 shows how to add an element to the current document.
<html>
<script language="JScript">
<!--
var myLineItemElement = documentObj.createElementNS
("http://foo.com/namespace", "foo:lineItem");
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
documentObj.createEntityReference(name)
The createEntityReference()
method of the Document
object creates and returns a new instance of an EntityReference
object given the name of the entity to reference. If the named entity is already known, the child list of the EntityReference
node is made the same as that of the corresponding entity. A DOMException
object can be raised with the value INVALID_CHARACTER_ERR if the given name contains an illegal character or the value NOT_SUPPORTED_ERR if this document is an HTML document.
Listing 10.20 illustrates using a previously created Document
object to create an entity reference.
<html>
<script language="JScript">
<!--
var entityReferenceObj = documentObj.createEntityReference("myRef");
if(entityReferenceObj.hasAttributes())
handleNoAttributes();
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
documentObj.createProcessingInstruction(target, data)
The createProcessingInstruction()
method of the Document
object creates an instance of a ProcessingInstruction
node with DOMStrings
target and data and returns it. A DOMException
object with value INVALID_CHARACTER_ERR
can be raised if the target contains an illegal character or with the value NOT_SUPPORTED_ERR
if this document is an HTML document.
Listing 10.21 illustrates adding a CDATA section to a previously created document.
<html>
<script language="JScript">
<!--
var cdataSectionObj = documentObj.createCDATASection("foo CDATA Section");
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
documentObj.createTextNode(data)
The createTextNode()
method of the Document
object uses the string data to create a new text node for this Document
. It returns a new instance of a Text
object that has its data attribute set to data.
Listing 10.22 uses a previously created Document
object to create a new text node in the current document.
Method of the Document
Object
<html>
<script language="JScript">
<!--
var textObj = documentObj.createTextNode("Some new text.");
var splitText = textObj.splitText(4);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
documentObj.docType
The doctype
property of the Document
object returns the document type declaration associated with this document. This attribute is read only and cannot be manipulated with methods inherited from Node
such as insertNode()
or removeNode()
. The return type is DocumentType
. For HTML documents, this property returns Null.
Listing 10.23 shows how to check the document type declaration of the current document.
<html>
<script language="JScript">
<!--
var docTypeObj = documentObj.docType;
if(docTypeObj == null)
handleHTML();
else
handleXML();
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
documentObj.documentElement
The read-only property documentElement
of the Document
object allows direct access to the root element of this document. It’s return value is type Element
.
Listing 10.24 illustrates using the Document
object to retrieve the root element of the current document.
<html>
<script language="JScript">
<!--
var rootElementObj = documentObj.documentElement;
var hasFooAttribute = rootElementObj.hasAttribute("foo");
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
documentObj.getElementById(elementId)
The getElementById()
method of the Document
object returns the Element
whose ID is specified by elementId in this Document
.
Listing 10.25 shows how to retrieve an element using its element identifier.
<html>
<script language="JScript">
<!--
var myElement = documentObj.getElementById("12932945");
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
documentObj.getElementsByTagName(tagName)
The getElementsByTagName()
method of the Document
object returns a NodeList
of all Elements
with the specified tagName. The Elements
returned in the NodeList
occur in the order in which they were encountered in the Document
. If tagName is set to *
, the returned NodeList
will contain a list of all Elements
in this document.
Listing 10.26 illustrates getting all elements in a previously created Document
object and iterating through the list.
<html>
<script language="JScript">
<!--
var elementListObj = documentObj.getElementsByTagName("item");
var i = 0;
while ( i < elementListObj.length) {
processElementValue(elementListObj.item(i).nodeValue);
}
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
documentObj.getElementByTagNameNS(namespaceURI, localName)
The getElementsByTagNameNS()
method of the Document
object returns a NodeList
of all Elements
in this Document
with the given localName in the given name space as specified in nameSpaceURI in the order in which they are encountered in a preorder traversal of the Document
.
Listing 10.27 shows how to retrieve all elements in a document given a tag name.
<html>
<script language="JScript">
<!--
var elementListObj = documentObj.getElementsByTagName
("http://foo.com/namespace", "foo:item");
var i = 0;
while ( i < elementListObj.length) {
processElementValue(elementListObj.Item(i).NodeValue);
}
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
documentObj.implementation
The read-only property implementation of the Document
object returns the DOMImplementation
object associated with this document. This property is simply here for convenience.
Listing 10.28 illustrates using a Document
object to return the current DOMImplementation
.
<html>
<script language="JScript">
<!--
var domImplObj = documentObj.implementation;
var hasHTMLEvents = domImplObj.hasFeature("HTMLEvents", "2.0");
//-->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
documentObj.importNode(importedNode, deep)
The importNode()
method of the Document
object imports a Node
specified in the parameter importNode from another document into this document and returns it. The returned Node
has its parentNode attribute set to null. The importNode is not removed from the original document; it is simply copied into this document. If deep is set to true, the subtree beneath importNode will also be copied from the source document recursively. A DOMException
object can be raised with value NOT_SUPPORTED_ERR
if this document is an HTML document.
Listing 10.29 shows a previously created node being imported into the current document and then made a child of an element.
<html>
<script language="JScript">
<!--
var importedNode = documentObj.importNode(newNode, true);
elementObj.append(importedNode);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
Core DOM object.
A DocumentFragment
object represents a portion of an XML document’s tree. It differs from Document
in that it is lightweight in nature. It is useful for rearranging document structure in a simple fashion. It can be seen as analogous to the clipboard in cut-and-paste scenarios. The DocumentFragment
object inherits all methods and properties of its parent Node
.
Listing 10.30 illustrates how a Document
object is used to create a new DocumentFragment
.
<html>
<script language="JScript">
<!--
var docFragmentObj = documentObj.createDocumentFragment();
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
Core DOM object.
The DocumentType
object provides access to the list of entities that are defined in the XML document’s DTD. DocumentType
inherits all properties and methods from Node
. Table 10.4 lists all properties of the DocumentType
object.
A DocumentType
object is created using the CreateDocumentType()
method of the DOMImplementation
object. The DocumentType
represents the actual DTD used for building XML documents. It inherits all properties and methods of Node
.
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
documentTypeObj.entities
The read-only property entities
of the DocumentType
object returns a NamedNodeMap
containing external and internal entities declared in the DTD.
Listing 10.31 iterates through a list of entities and prints out their notation names.
<html>
<script language="JScript">
<!--
var entityList = docTypeObj.entities;
var i = 0;
while( i < entityList.length) {
writeEntityName(entityList.item(i).notationName);
}
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
documentTypeObj.InternalSubset
The read-only property internalSubset
of the DocumentType
object returns a string representation of the internal subset.
Listing 10.32 retrieves the internal subset from a DocumentType
object.
<html>
<script language="JScript">
<!--var internalSubset = docTypeObj.internatSubset;
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
documentTypeObj.name
The read-only property name
of the DocumentType
object returns the name of this DTD.
Listing 10.33 illustrates retrieving the Name of a DTD using the name
property of the DocumentType
object.
<html>
<script language="JScript">
<!--
var docTypeName = docTypeObj.name;
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
documentTypeObj.notations
The read-only property notations
of the DocuementType
object returns a NamedNodeMap
containing all notations declared in the DTD.
Listing 10.34 iterates through a list of notations and writes out their public identifiers.
<html>
<script language="JScript">
<!--
var notationList = docTypeObj.notations;
var i = 0;
while( i < entityList.length) {
writeEntityName(entityList.item(i).publicId);
}
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
documentTypeObj.publicId
The read-only property publicId
of the DocumentType
object returns the public identifier of the external subset.
Listing 10.35 retrieves the public identifier from a DocumentType
object.
<html>
<script language="JScript">
<!--
var publicId = docTypeObj.publicId;
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
documentTypeObj.systemId
The read-only property systemId
of the DocumentType
object returns the system identifier of the external subset.
Listing 10.36 retrieves the system identifier from a DocumentType
object.
<html>
<script language="JScript">
<!--
var systemId = docTypeObj.systemId;
// -->
</script>
</html>
JavaScript1.5+, JScript5.0+
Core DOM object.
The DOMException
object encapsulates all exception situations that are seen throughout the Core DOM DOM Level 2 API. Table 10.5 lists all of the exception types as well as the property of the DOMException
object.
Listing 10.37 creates a new DOMException
object with code
value NAMESPACE_ERR and throws it.
<html>
<script language="JScript">
<!--
var domExceptionObj = new DOMException(DOMException.NAMESPACE_ERR);
throw domExceptionObj;
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
domExceptionObj.code
The code
property of the DOMException
object returns a number indicating which exception has occurred.
Listing 10.38 illustrates catching a DOMException
object and interrogating the code
property to handle the appropriate error.
<html>
<script language="JScript">
<!--
var domImplObj = new DOMImplementation();
try {
var documentObj = domImplObj.createDocument
("http://foobar.org/schema", "foo:bar", null);
}
catch(e if e == "DOMException") {
if (e.code == DOMException.INVALID_CHARACTER_ERR)
handleError(e)
}
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
DOM Core object.
DOMImplementation
is used to create XML documents and document types in a non-implementation specific manner. The object provides several convenient methods for performing tasks independent of the DOM implementation used. Table 10.6 lists all methods of the DOMImplementation
object.
Listing 10.39 creates a DOMImplementation
object and creates a Document
.
<html>
<script language="JScript">
<!--
var domImplObj = new DOMImplementation();
var documentObj = domImplObj.createDocument
("http://foobar.org/schema", "foo:bar", null);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
DOMImplementation.CreateDocument(namespaceURI, qualifiedName, doctype)
The createDocument()
method of the DOMImplementation
object takes the two DOMString
arguments namespaceURI and qualifiedName along with the DocumentType
argument doctype to create and return a new XML Document
object of the specified type. This method raises a DOMException
object with its code set to the following:
INVALID_CHARACTER_ERR if
qualifiedName contains an illegal character
NAMESPACE_ERR
if qualifiedName is malformed or qualifiedName if prefixed
with xml
and namespaceURI is something other thanhttp://www.w3.org/XML/1998/namespace
WRONG_DOCUMENT_ERR
if doctype has already been used with another document or
was created from a different DOM implementation
Listing 10.40 uses a DOMImplementation
object to create a new Document
.
<html>
<script language="JScript">
<!--
var domImplObj = new DOMImplementation();
var documentObj = domImplObj.createDocument
("http://foobar.org/schema", "foo:bar", null);
//-->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
DOMImplementation.createDocumentType
(qualifiedName, publicId, systemId)
The createDocumentType()
method of the DOMImplementation
object takes the three string
arguments qualifiedName,publicId, and systemId and returns a new empty DocumentType
object. This method raises a DOMException
with its code set to INVALID_CHARACTER_ERR
if qualifiedName contains an illegal character or NAME-SPACE_ERR
if qualifiedName is malformed.
Listing 10.41 uses a DOMImplementation
object to create a new empty XML document type.
<html>
<script language="JScript">
<!--
var domImplementation = new DOMImplementation();
var docTypeObj = domImplObj.createDocumentType("foo:bar",
"http://foobar.com/foo.xml","http://foobar.com/bar.xml");
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
DOMImplementation.hasFeature (feature, version)
The hasFeature()
method of the DOMImplementation
object takes the DOMString
arguments feature and version and returns a Boolean value indicating whether the feature is available in the DOMImplementation
implementation.
Listing 10.42 uses a DOMImplementation
object to check whether level 2.0 contains support for HTML Events.
<html>
<script language="JScript">
<!--
var domImplObj = new DOMImplementation();
var hasHTMLEvents = domImplObj.hasFeature("HTMLEvents", "2.0");
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
Core DOM object.
The Element
object represents an element in an XML or HTML document. An Element
object inherits all methods and properties from Node
. Table 10.7 lists all properties and methods of the Element
object.
Listing 10.43 creates an Element
from a Document
object.
<html>
<script language="JScript">
<!--
var newElementObj = documentObj.createElement("item");
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
elementObj.getAttribute(name)
The getAttribute()
method of the Element
object returns the string value for the attribute name.
Listing 10.44 gets a quantity attribute from an element representing a line item on a purchase order.
<html>
<script language="JScript">
<!--
var qtyAttrValue = elementObj.getAttribute("quantity");
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
elementObj.getAttributeNode(name)
The getAttributeNode()
method of the Element
object retrieves the attribute specified by name
and returns it as an Attr
. If there is no such Attr
object,null is returned.
Listing 10.45 gets an attribute node from an element and sets its value.
<html>
<script language="JScript">
<!--
var qtyAttr = elementObj.getAttributeNode("quantity");
qtyAttr.value = "3";
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
elementObj.getAttributeNodeNS(namespaceURI, localName)
The getAttributeNodeNS()
method of the Element
object returns the Attr
value containing the specified namespaceURI and localName.
Listing 10.46 retrieves an attribute node representing a quantity from an element representing a line item.
<html>
<script language="JScript">
<!--
var qtyAttrNodeObj = invoiceElementObj.getAttributeNodeNS
("http://foo.com/namespace", "foo:quantity");
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
elementObj.getAttributeNS(namespaceURI, localName)
The getAttributeNS()
method of the Element
object returns the string value of the attribute specified by the given namespaceURI and localName.
Listing 10.47 gets an element’s attribute using namespace URI and local name.
<html>
<script language="JScript">
<!--
var qtyAttrValue = elementObj.getAttributeNS
("http://foo.com/namespace", "foo:quantity");
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
elementObj.getElementsByTagName(name)
The getElementsByTagName()
method of the Element
object returns a NodeList
containing all Elements
named name.
Listing 10.48 gets a list of line items from an element representing an invoice.
<html>
<script language="JScript">
<!--
var lineItemList = invoiceElementObj.getElementsByTagName("lineItem");
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
elementObj.getElementsByTagNameNS(namespaceURI, localName)
The getElementsByTageNameNS()
method of the Element
object returns a NodeList
of all descendant Elements
with the given namespaceURI and localName.
Listing 10.49 gets a list of line items from an element representing an invoice.
<html>
<script language="JScript">
<!--
var lineItemList = invoiceElementObj.getElementsByTagNameNS
("http://foo.com/namespace", "lineItem");
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
elementObj.hasAttribute(name)
The hasAttribute() method of the Element object returns true
if this element has an attribute named name.
Listing 10.50 determines whether a line item element has an attribute for price set.
<html>
<script language="JScript">
<!--
if(!lineItemElementObj.hasAttribute("price"))
lineItemElementObj.setAttribute("price", "3.44");
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
elementObj.hasAttributeNS(namespaceURI, localName)
The hasAttributeNS()
method of the Element
object returns True
if there exists an attribute of this element that has the specified namespaceURI and localName.
Listing 10.51 determines whether a line item element has an attribute for price set.
<html>
<script language="JScript">
<!--
if(!lineItemElementObj.hasAttributeNS("http://foo.com/namespace", "price"))
lineItemElementObj.setAttribute("price", "3.44");
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
elementObj.removeAttribute(name)
The removeAttribute()
method of the Element
object removes the named attribute specified by the string name from this element.
Listing 10.52 removes the quantity attribute from an element representing a line item.
<html>
<script language="JScript">
<!--
elementObj.removeAttribute("quantity");
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
elementObj.removeAttributeNode(oldAttr)
The removeAttributeNode()
method of the Element
object removes oldAttr. A DOMException
object is raised with the value NO_MODIFICATION_ALLOWED_ERR if this node is read-only or with value NOT_FOUND_ERR if oldAttr isn’t an attribute of this element.
Listing 10.53 Removes an attribute node from an element.
<html>
<script language="JScript">
<!--
elementObj.removeAttributeNode(oldNode);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
elementObj.removeAttributeNS(namespaceURI, localName)
The removeAttributeNS()
method of the Element
object removes the attribute specified by the given namespaceURI and localName. A DOMException
object can be raised with value NO_MODIFICATION_ALLOWED_ERR if this node is read-only.
Listing 10.54 removes the quantity attribute from an element representing an invoice.
<html>
<script language="JScript">
<!--
invoiceElementObj.removeAttributeNS
("http://foo.com/namespace", "foo:quantity");
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
elementObj.setAttribute(name, value)
The setAttribute()
method of the Element
object adds a new attribute named name to value. If the attribute is already present, its value is changed to value. A DOMException
object can be raised with value INVALID_CHARACTER_ERR if the specified name contains an illegal character or with a value NO_MODIFICATION_ALLOWED_ERR if this node is read-only.
Listing 10.55 sets a quantity attribute for an element that represents a line item.
<html>
<script language="JScript">
<!--
elementObj.setAttribute("quantity", "3");
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
elementObj.setAttributeNode(newAttr)
The setAttributeNode()
method of the Element
object adds a new attribute node newAttr. The newly placed Attr
is returned. If newAttr is already an attribute of this element, Null is returned.
Listing 10.56 sets a attribute node representing a quantity for an element representing a line item.
<html>
<script language="JScript">
<!--
var qtyAttr = documentObj.createAttribute("quantity");
qtyAttr.value = "3";
elementObj.setAttributeNode(qtyAttr);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
elementObj.setAttributeNodeNS(newAttr)
The setAttributeNodeNS()
method of the Element
object adds a new attribute newAttr. If an attribute with matching local name and namespace URI already exists, it will be replaced. A DOMException
object can be raised with value WRONG_DOCUMENT_ERR if newAttr was created by a document other than the one that created this element or with value INUSE_ATTRIBUTE_ERR if newAttr is already an attribute of another element. In this case, the newAttr needs to be cloned for reuse.
Listing 10.57 sets a new quantity attribute for an element representing a line item.
<html>
<script language="JScript">
<!--
var qtyAttrObj = documentObj.createAttribute("quantity");
lineItemElementObj.setAttributeNodeNS(qtyAttrObj);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
elementObj.setAttributeNS(namespaceURI, qualifiedName, value)
The setAttributeNS()
method of the Element
object adds a new attribute to this element. If an attribute with the same namespaceURI and qualifiedName already exists, its value will be replaced with the new value.
Listing 10.58 sets an attribute using its namespace URI.
<html>
<script language="JScript">
<!--
invoiceElementObj.setAttributeNS
("http://foo.com/namespace", "foo:quantity", "4");
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
elementObj.tagName
The read-only property tagName
of the Element
object returns the name of this element.
Listing 10.59 queries an Element
for its tagName.
<html>
<script language="JScript">
<!--
var tagName = elementObj.tagName;
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
Core DOM object.
The Entity
object represents an XML entity that is either parsed or unparsed. It inherits all methods and properties of Node
. Table 10.8 lists the properties for the Entity
object.
An Entity
is instantiated using the createEntity()
method of the Document
object. Entity inherits all methods and properties from the Node
object.
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
entityObj.notationName
The read-only property notationName
of the Entity
object returns the name of the notation for this entity if it is unparsed. If the entity is parsed, this returns null.
Listing 10.60 retrieves the notation name from a Entity
object.
<html>
<script language="JScript">
<!--
var notationName = entityObj.notationName;
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
entityObj.publicId
The read-only property publicId
of the Entity
object returns the public identifier associated with this entity. If it isn’t specified,null is returned.
Listing 10.61 retrieves the public identifier from an Entity
object.
<html>
<script language="JScript">
<!--
var publicId = entityObj.publicId;
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
entityObj.systemId
The read-only property systemId
of the Entity
object returns the system identifier associated with this entity. If the system ID isn’t specified, this returns null.
Listing 10.62 retrieves the system identifier from an Entity
object.
<html>
<script language="JScript">
<!--
var systemId = entityObj.systemId;
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
Core DOM object.
An EntityReference
object inherits all methods and properties from Node
.
An EntityReference
is instantiated using the createEntityReference()
method from the Document
object.
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
Core DOM object
This is a list of DOM nodes with names associated with each. Table 10.9 lists all properties and methods of the NamedNodeMap
object.
Listing 10.63 retrieves the NamedNodeList attributes
property from a HTMLBodyElement
object and reads its bgColor
property.
<html>
<script language="JScript">
<!--
var bodyAtts = bodyObj.attributes;
var color = bodyAtts.bgColor;
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
namedNodeMapObj.getNamedItem(name)
The getNamedItem()
method of the NamedNodeMap
object returns a node specified by name.
Listing 10.64 retrieves an attribute object by name from a NamedNodeMap
and processes it.
<html>
<script language="JScript">
<!--
var attrList = nodeObj.attributes;
var size = attrList.getNamedItem("size");
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
namedNodeMap.getNamedItemNS(namespaceURI, localName)
The getNamedItemNS()
method of the NamedNodeMap
object returns the node specified by localName and namespaceURI. This method returns Null if no node exists for the specified localName or namespaceURI.
Listing 10.65 retrieves an attribute object by localName
and a namespace URI from a
NamedNodeMap and processes it.
<html>
<script language="JScript">
<!--
var attrList = nodeObj.attributes;
var size = attrList.getNamedItemNS("http://foo.com/namespace", "foo:size");
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
namedNodeMapObj.item(index)
The item()
method of the NamedNodeMap
object returns the node in position index in this map. If no node exists at this position,null is returned.
Listing 10.66 iterates through an attribute list processing each one.
<html>
<script language="JScript">
<!--
var attrList = nodeObj.attributes;
var i = 0;
while(i < attrList.length) {
processAttr(attrList.item(i));
i++;
}
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
namedNodeMapObj.length
The length
property of the NamedNodeMap
object returns the number of nodes contained in this named node map.
Listing 10.67 checks to see whether a node has any attributes. If it does, the attributes are iterated over and processed. The attrList is an example of a NamedNodeMap
.
<html>
<script language="JScript">
<!--
if(nodeObj.hasAttributes()) {
var attrList = nodeObj.attributes;
var i = 0;
while(i < attrList.length) {
processAttr(attrList.Item(i);
i++;
}
}
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
namedNodeMapObj.removeNamedItem(name)
The removeNamedItem()
method of the NamedNodeMap
object removes the node specified by name
and returns it. A DOMException
can be raised with the value NOT_FOUND_ERR
if no name
node is in this map, or with value NO_MODIFICATION_ALLOWED_ERR
if this map is read-only.
Listing 10.68 removes a named item from an attribute list.
<html>
<script language="JScript">
<!--
attrList.removeNamedItem("size");
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
namedNodeMapObj.removeNamedItemNS(name)
The removeNamedItemNS()
method of the NamedNodeList
object removes a node specified by name
and returns it. If no node exists with the name specified, null
is returned.
Listing 10.69 removes an item from an attribute list.
<html>
<script language="JScript">
<!--
attrListObj.removeNamedItemNS("size");
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
nameNodeMapObj.setNamedItem(node)
The setNamedItem()
method of the NamedNodeMap
object adds a node
using its
NodeName property. If a node is already present in this named node map with the same name, it will be replaced. The newly added node is returned. A DOMException
object can be raised with one of the following values:
WRONG_DOCUMENT_ERR
if the node was created from a different document than the one that created this map
NO_MODIFICATION_ALLOWED_ERR
if this map is read-only
INUSE_ATTRIBUTE_ERR
if node is an Attr
that is already an attribute of another Element
object. If this is the case, the DOM user must clone the Attr
for reuse
Listing 10.70 sets a new named attribute in an attribute list.
<html>
<script language="JScript">
<!--
var newAttr = documentObj.createAttribute("size");
attrList.setNamedItem(newAttr);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
namedNodeMapObj.setNamedItemNS(node)
The setNamedItemNS()
method of the NamedNodeMap
object adds a node
to the map using its namespaceURI
and localName
properties as keys on the map. If a node already exists at the specified keys, it is replaced. A DOMException
object can be raised with one of the following values:
WRONG_DOCUMENT_ERR
if the node was created by a document other than the one that created this map
NO_MODIFICATION_ALLOWED_ERR
if this map is read-only
INUSE_ATTRIBUTE_ERR
if node is an Attr
that is already an attribute of another Element
object. In this case, the DOM user must explicitly clone the Attr
node for reuse
Listing 10.71 sets a new named attribute in an attribute list.
<html>
<script language="JScript">
<!--
var newAttr = documentObj.createAttribute("size");
attrList.setNamedItemNS(newAttr);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
Core DOM object.
The Node
object represents a node of an XML document. Table 10.10 lists all constants, properties, and methods of the Node
object.
Because Node
is a parent object to all DOM Core objects, it is never instantiated on its own. Instead, its methods and properties are accessed via its child objects.
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
nodeObj.attributes
The read-only property attributes
of the Node
object returns a NamedNodeMap
containing all attributes of this node.
Listing 10.72 iterates through a list of node attributes and processes them.
<html>
<script language="JScript">
<!--
var attrList = nodeObj.attributes;
var i = 0;
while(i < attrList.length) {
processAttr(attrList.item(i);
i++;
}
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
nodeObj.appendChild(newChild)
The appendChild()
method of the Node
object adds newChild
to the list of children for this node. If newChild
is already in the tree, it is first removed. The newly added child node is returned. A DOMException
can be raised with the value HIERARCHY_REQUEST_ERR
if this node is of a type that doesn’t accept children of the newChild
type, with value WRONG_DOCUMENT_ERR
if newChild
was created by a document other than the one that created this node, or with value NO_MODIFICATION_ALLOWED
if this node is read-only.
Listing 10.73 adds a new child to a node.
<html>
<script language="JScript">
<!--
var newElementObj = documentObj.createElement("item");
nodeObj.appendChild(newElementObj);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
nodeObj.childNodes
The childNodes
attribute of the Node
object returns a NodeList
containing all children of this node.
Listing 10.74 returns all children of a node and then iterates through the list, printing out the text contents if the node is a text node.
<html>
<script language="JScript">
<!--
var nodeListObj = nodeObj.childNodes;
var i = 0;
while(i < nodeListObj.Length) {
if(nodeListObj.item(i).nodeType == Node.TEXT_NODE)
handleTextNode(nodeListObj.Item(i));
i++;
}
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
nodeObj.cloneNode(deep)
The cloneNode()
method of the Node
object returns a duplicate copy of this node. If deep
is true, the subtree under this node is cloned along with this node.
Listing 10.75 clones a node and then adds the clone as a child to another node.
<html>
<script language="JScript">
<!--
var clonedNodeObj = nodeObj.cloneNode(true);
anotherNodeObj.appendChild(clonedNodeObj);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
nodeObj.firstChild
The read-only property firstChild
of the Node
object returns the first child of this node.
Listing 10.76 checks to see the type of the first child of this node.
<html>
<script language="JScript">
<!--
var childNodeObj = nodeObj.firstChild;
if(childNodeObj.nodeName == "foo")
process(childNodeObj);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
nodeObj.hasAttributes()
The hasAttributes()
method of the Node
object returns True
if this node has any attributes attached to it.
Listing 10.77 checks to see whether a node has any attributes and iterates the list if it does.
<html>
<script language="JScript">
<!--
if(nodeObj.hasAttributes()) {
var attrList = nodeObj.attributes;
var i = 0;
while(i < attrList.Length) {
processAttr(attrList.Item(i);
i++;
}
}
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
nodeObj.hasChildNodes()
The hasChildNodes()
method of the Node
object returns true
if this node has children.
Listing 10.78 tests to see if a node has children.
<html>
<script language="JScript">
<!--
if(nodeObj.hasChildNodes())
handleChildren(nodeObj);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
nodeObj.insertBefore(newChild, refChild)
The insertBefore()
method of the Node
object inserts the node newChild
before the existing child node refChild
. If newChild
is already in the tree, it is first removed. The newly inserted child node is returned. A DOMException
object can be raised with one of the following values:
HIERARCHY_REQUEST_ERR
if this node is a type that doesn’t allow children of the newChild
type
WRONG_DOCUMENT_ERR
if the newChild
was created from a different document than the one that created this node
NO_MODIFICATION_ALLOWED_ERR
if this node is read-only
NOT_FOUND_ERR
if refChild
isn’t a child of this node
Listing 10.79 inserts a new child node before a reference child node.
<html>
<script language="JScript">
<!--
var newElementObj = documentObj.createElement("item");
nodeObj.insertBefore(newElementObj, existingChild);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
nodeObj.isSupported(feature, version)
The isSupported()
method of the Node
object returns whether the specified feature
and version
are supported by this node.
Listing 10.80 queries a node for the availability of a certain feature.
<html>
<script language="JScript">
<!--
if(nodeObj.isSupported("HTMLEvents", "1.0")
handleHTMLEvents();
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
nodeObj.lastChild
The read-only property lastChild
of the Node
object returns the last child of this node.
Listing 10.81 checks to see the type of the last child of this node.
<html>
<script language="JScript">
<!--
var childNodeObj = nodeObj.lastChild;
if(childNodeObj.nodeName == "foo")
process(childNodeObj);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
nodeObj.localName
The read-only property localName
of the Node
object returns the local part of the qualified name for this node.
Listing 10.82 takes the local name of a node and prepends the prefix to give the qualified name of the node.
<html>
<script language="JScript">
<!--
var localName = nodeObj.localName;
var prefix = nodeObj.prefix;
var qualifiedName = prefix+":"+localName;
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
nodeObj.namespaceURI
The read-only property namespaceURI
of the Node
object returns a string
containing the namespace URI for this node.
Listing 10.83 checks the prefix against the namespace URI and throws an exception if the prefix is foo
and the namespace URI is not http://foo.com/namespace
.
<html>
<script language="JScript">
<!--
if((nodeObj.Prefix == "foo" )
&& (nodeObj.NamespaceURI != "http://foo.com/namespace"))
throw new DOMException(DOMException.NAMESPACE_ERR);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
nodeObj.nextSibling
The read-only property nextSibling
of the Node
object returns the node immediately after this node.
Listing 10.84 checks to see the type of the next sibling of this node.
<html>
<script language="JScript">
<!--
var siblingNodeObj = nodeObj.nextSibling;
if(siblingNodeObj.nodeName == "foo")
process(siblingNodeObj);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
nodeObj.nodeName
The nodeName
property of the Node
object returns the name of this node as indicated by the nodeType
attribute.
Listing 10.85 takes a Node
and performs a particular operation depending on the nodeName
.
<html>
<script language="JScript">
<!--
if(elementObj.nodeName == "foobar")
processElement(elementObj);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
nodeObj.nodeType
The nodeType
property of the Node
object returns a number indicating the node type of this node as represented by the constants listed previously.
Listing 10.86 checks the type of a node and processes it accordingly.
<html>
<script language="JScript">
<!--
var nodeType = textObj.nodeType;
if(nodeType == Node.TEXT_NODE)
handleText(textObj);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
nodeObj.nodeValue
The nodeValue
property of the Node
object returns the value of this node depending on its nodeType
attribute
Listing 10.87 illutstrates iterating through a list of elements processing each object’s nodeValue
property.
<html>
<script language="JScript">
<!--
var elementListObj = documentObj.getElementsByTagName
("http://foo.com/namespace", "foo:item");
var i = 0;
while ( i < elementListObj.length) {
processElementValue(elementListObj.item(i).nodeValue);
}
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
nodeObj.normalize()
The normalize()
method of the Node
object puts all Text
nodes in the full depth of the subtree underneath this node. This can be used to ensure that the DOM view of the document is the same as if it were saved and reloaded.
Listing 10.88 demonstrates normalizing a node before saving it to a file.
<html>
<script language="JScript">
<!--
nodeObj.normalize();
writeNode(nodeObj);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
nodeObj.ownerDocument
The read-only property ownerDocument
of the Node
object returns the Document
object associated with this node.
Listing 10.89 retrieves the owner document of this node and creates a new element.
<html>
<script language="JScript">
<!--
var documentObj = nodeObj.ownerDocument;
var elementObj = documentObj.createElement("lineItem");
nodeObj.appendChild(elementObj);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
nodeObj.parentNode
The parentNode
property of the Node
object returns the parent of this node.
Listing 10.90 checks to see whether a node object has a parent.
<html>
<script language="JScript">
<!--
if(elementObj.parentNode != null)
processAsChild(elementObj);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
nodeObj.prefix
The prefix
property of the Node
object returns the namespace prefix for this node. A DOMException
object can be raised with one of the following values:
INVALID_CHARACTER_ERR
if the prefix contains an illegal character
NO_MODIFICATION_ALLOWED_ERR
if the node is read-only
NAMESPACE_ERR
if the prefix is malformed, the NamespaceURI
is Null, or the prefix is xml
and the NameSpaceURI
isn’t http://www.w3.org/XML/1998/namespace
Listing 10.91 checks the prefix against the namespace URI and throws an exception if the prefix is foo
and the namespace URI isn’t http://foo.com/namespace
.
<html>
<script language="JScript">
<!--
if((nodeObj.prefix == "foo")
&& (nodeObj.namespaceURI != "http://foo.com/namespace"))
throw new DOMException(DOMException.NAMESPACE_ERR);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
nodeObj.previousSibling
The read-only property previousSibling
of the Node
object returns the node immediately before this node.
Listing 10.92 illustrates checking to see the type of the previous sibling of this node.
<html>
<script language="JScript">
<!--
var siblingNodeObj = nodeObj.previousSibling;
if(siblingNodeObj.nodeName == "foo")
process(siblingNodeObj);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
nodeObj.removeChild(oldChild)
The removeChild()
method of the Node
object removes the child node specified by oldChild
from the list of children of this node. A DOMException
object can by raised with value NO_MODIFICATION_ALLOWED_ERR
if this node is read-only, or with value NOT_FOUND_ERR
if oldChild
isn’t a child of this node.
Listing 10.93 removes a child from a node.
<html>
<script language="JScript">
<!--
nodeObj.removeChild(existingChild);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
nodeObj.replaceChild(newChild, oldChild)
The replaceChild()
method of the Node
object replaces the child node oldChild
with a new child node newChild
. If newChild
is already in the tree, it is first removed. A DOMException
object is raised with one of the following values:
HIERARCHY_REQUEST_ERR
if this node is of a type that doesn’t allow children of the newChild
type
WRONG_DOCUMENT_ERR
if the newChild
was created from a different Document than the one that created this node
NO_MODIFICATION_ALLOWED_ERR
if this node or the parent of the new node is read-only
NOT_FOUND_ERR
if the oldChild
isn’t a child of this node
Listing 10.94 replaces an existing child node with a new node.
<html>
<script language="JScript">
<!--
var newElementObj = documentObj.createElement("item");
nodeObj.replaceChild(newElementObj, existingChild);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
Core DOM object.
Notation
inherits all properties and methods from its parent class Node
. A Notation
declares an unparsed entity in an XML document. Table 10.11 lists all properties of the Notation
object.
A Notation
is instantiated using the createNotation()
method of the Document
object.
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
notationObj.publicId
The read-only property publicId
of the Notation
object returns the public identifier of this notation. If the PublicId
is not specified for this notation, it returns null
.
Listing 10.95 retrieves the public identifier from a Notation
object.
<html>
<script language="JScript">
<!--
var publicId = docTypeObj.publicId;
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
notationObj.systemId
The read-only property systemId
of the Notation
object returns the system identifier of this notation. If the systemId
isn’t specified for this notation, it returns null
.
Listing 10.96 retrieves the system identifier from a Notation
object.
<html>
<script language="JScript">
<!--
var systemId = docTypeObj.systemId;
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
Core DOM object.
ProcessingInstruction
inherits all properties and methods from Node
. ProcessingInstruction
represents an XML processing instruction that is used to keep parser specific information in the text of the XML document. Table 10.12 lists the properties of the ProcessingInstruction
object.
A ProcessingInstruction
is instantiated using the createProcessingInstruction()
method of the Document
object.
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
processingInstructionObj.data
The read-only property data
of the ProcessingInstruction
object returns the content of the processing instruction. A DOMException
object can be raised with the value NO_MODIFICATION_ALLOWED_ERR
when this node is read-only.
Listing 10.97 illustrates the retrieval of the data
property.
<html>
<script language="JScript">
<!--
var instructionData= processingInstructionObj.data;
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
processingInstructionObj.target
The readonly property target
if the ProcessingInstruction
object returns the target of this processing instruction.
Listing 10.97 illustrates the retrieval of the target
property.
<html>
<script language="JScript">
<!--
var targetToken = processingInstructionObj.target;
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
Core DOM object.
A Text
object represents textual content in an XML document. If no markup is present in an element’s content, it is stored as a Text
object. Text
inherits all methods and properties of CharacterData
, which, in turn, inherits from Node
. An argument associated with the Text
object is as follows:
A Text
object is created by the createTextNode()
method of the Document object
.
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
textObj.splitText(offset)
The splitText()
method of the Text
object breaks this text node into two nodes at the given offset
. After the text is split, the new node becomes a sibling of this node and will contain all the text after the offset
. The new Text
node is returned. A DOMException
can be raised with the value INDEX_SIZE_ERR
if the specified offset
is negative or greater than the size of the contained text data or with the value NO_MODIFICATION_ALLOWED_ERR
if this node is read-only.
Listing 10.98 demonstrates splitting a Text node’s Data into another Text
node.
<html>
<script language="JScript">
<!--
var textObj = documentObject.createTextNode("This is some text");
var splitTextObj = textObj.splitText(5);
// -->
</script>
</html>
18.118.37.254