Chapter 10 DOM Core

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.

Attr

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

Core DOM object.

Description

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.

Table 10.1 Arguments Associated With Attr Object

Image

Example

An Attr is created by using the CreateAttribute() method of the Document object.

Attr.Name

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

attrObj.name

Description

The name property of the Attr object returns the name of this attribute.

Example

Listing 10.1 iterates through a list of attributes and writes out each attribute’s name.

Listing 10.1 Writing an Attribute’s Name Using the name Property of the Attr Object

<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>

Attr.specified

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

attrObj.specified

Description

The read-only property specified of the Attr object returns true if this attribute’s value was explicitly assigned in the original document.

Example

Listing 10.2 iterates through a list of attributes checking to see which ones were specified in the original XML document.

Listing 10.2 Checking if Attribute Was Specified Using the specified Property of the Attr Object

<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>

Attr.value

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

Syntax

attrObj.Value

Description

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.

Example

Listing 10.3 checks the value of a size attribute.

Listing 10.3 Checking the Value of an Attribute Using the value Property of the Attr Object

<html>
<script language="JScript">
<!--
var sizeValue = sizeAttrObj.value;
// -->
</script>
</html>

Attr.ownerElement

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

attrObj.ownerElement

Description

The ownerElement property of the Attr object returns the Element node that this attribute is attached to.

Example

Listing 10.4 retrieves the owner element for an attribute and then removes the attribute from the element.

Listing 10.4 Retrieving the Owner Element Using the ownerElement Property of the Attr Object

<html>
<script language="JScript">
<!--
var elementObj = attrObj.ownerElement;
elementObj.removeAttribute(attrObj);
// -->
</script>
</html>

CDATASection

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

Core DOM object.

Description

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.

Example

A CDATASection object is instantiated using the CreateCDATASection() method of the Document object.

CharacterData

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

Syntax

Core DOM object.

Description

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.

Table 10.2 Arguments Associated with CharacterData Object

Image

Example

CharacterData objects are never directly instantiated. They exist through subclasses such as Text and Comment.

CharacterData.data

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

characterDataObj.data

Description

The readonly property data of the CharacterData object returns the character data of this node.

Example

Listing 10.5 retrieves string data from a Text node and displays it.

Listing 10.5 Retrieving String Data from a Text Node Using the data Property of the CharacterData Object

<html>
<script language="JScript">
<!--
var textData = textObj.data;
write(textData);
// -->
</script>
</html>

CharacterData.length

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

characterDataObj.length

Description

The read-only property length of the CharacterData object returns the length of data in 16-bit units.

Example

Listing 10.6 queries a Text node for the length of its data field.

Listing 10.6 Checking Text Data Length Using the length Property of the CharacterData Object

<html>
<script language="JScript">
<!--
var dataSize = textObj.length;
if(dataSize > bufferSize)
    printError();
// -->
</script>
</html>

CharacterData.substringData()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

characterDataObj.substringData(offsetcount)

Description

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.

Example

Listing 10.7 creates a substring from the given Text node’s data.

Listing 10.7 Creating a Substring Using the substringData() Method of the CharacterData Object

<html>
<script language="JScript">
<!--
textObj.data = "Test Text Data";
var subStringData = textObj.subStringData(0, 4);
// -->
</script>
</html>

CharacterData.appendData()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

characterDataObj.appendData(arg)

Description

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.

Example

Listing 10.8 adds a string to the end of a Text node’s data property.

Listing 10.8 Appending Data Using the appendData() Method of the CharacterData Object

<html>
<script language="JScript">
<!--
textObj.data = "Test Text Data";
textObj.appendData("Some more data");
// -->
</script>
</html>

CharacterData.insertData()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

Syntax

characterDataObj.insertData(offsetarg)

Description

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.

Example

Listing 10.9 inserts a string into a Text node’s Data property.

Listing 10.9 Inserting Data Using the insertData() Method of the CharacterData Object

<html>
<script language="JScript">
<!--
textObj.data = "Test Text Data";
textObj.insertData(4, "Some more data");
// -->
</script>
</html>

CharacterData.deleteData()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

characterDataObj.deleteData(offsetcount)

Description

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.

Example

Listing 10.10 deletes a string from a Text node’s Data property.

Listing 10.10 Deleting Data Using the deleteData() Method of the CharacterData Object

<html>
<script language="JScript">
<!--
textObj.data = "Test Text Data";
textObj.deleteData(0, 5);
// -->
</script>
</html>

CharacterData.replaceData()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

Syntax

characterDataObj.replaceData(offsetcountarg)

Description

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.

Example

Listing 10.11 illustrates replacing one string for another in a Text node’s Data property.

Listing 10.11 Replacing Data Using the replaceData() Method of the CharacterData Object

<html>
<script language="JScript">
<!--
textObj.data = "Test Text Data";
textObj.replaceData(0, 4, "foo ");
// -->
</script>
</html>

Comment

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

Core DOM object.

Description

Comment inherits all methods and properties from the CharacterData object and represents the contents of an XML comment.

Example

A Comment is instantiated using the createComment() method of the Document object.

Document

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

Core DOM object.

Description

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.

Table 10.3 Arguments Associated with Document Object

Image

Example

Listing 10.12 uses a DOMImplementation object to create a Document object.

Listing 10.12 Creating a Document

<html>
<script language="JScript">
<!--
var domImplObj = new DOMImplementation();
var documentObj = domImplObj.
     createDocument("http://foobar.org/schema", "foo:bar", null);
//-->
</script>
</html>

Document.createAttribute()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

documentObj.createAttribute(name)

Description

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.

Example

Listing 10.13 illustrates adding a new attribute to the root element of a previously created document object.

Listing 10.13 Adding an Attribute with the createAttribute() Method

<html>
<script language="JScript">
<!--
var attrObj = documentObj.createAttribute("size");
var rootElement - documentObj.rootElement;
attrObj.value = 34;
rootElement.setAttributeNode(attrObj);
// -->
</script>
</html>

Document.createAttributeNS()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

documentObj.createAttributeNS(namespaceURIqualifiedName)

Description

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.

Example

Listing 10.14 illustrates creating an attribute using namespace references.

Listing 10.14 Adding an Attribute

<html>
<script language="JScript">
<!--
var newAttribute = documentObj.createAttributeNS
     ("http://foo.com/namespace", "foo:quantity");
newAttribute.Value = 10;
myLineItemElement.SetAttributeNode(newAttribute);
// -->
</script>
</html>

Document.createCDATASection()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

documentObj.createCDATASection(data)

Description

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.

Example

Listing 10.15 illustrates how to create and add a CDATA section to a previously created document.

Listing 10.15 Adding a New CDATA Section

<html>
<script language="JScript">
<!--
var cdataSectionObj = documentObj.createCDATASection("foo");
// -->
</script>
</html>

Document.createComment()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

documentObj.createComment(data)

Description

The createComment() method of the Document object creates and returns a new Comment node using the DOMStringdata.

Example

Listing 10.16 uses a previously created Document object to create and add a new Comment to the current XML document.

Listing 10.16 Adding a New Comment to an XML Document

<html>
<script language="JScript">
<!--
var commentObj = documentObj.createComment
     ("Here is an example comment.");
// -->
</script>
</html>

Document.createDocumentFragment()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

documentObj.createDocumentFragment()

Description

The createDocumentFragment() method of the Document object creates an empty DocumentFragment object.

Example

Listing 10.17 uses a Document object to create a new DocumentFragment.

Listing 10.17 Creating a Document Fragment Using the

<html>
<script language="JScript">
<!--
var documentFragmentObj = documentObj.createDocumentFragment();
var nodeType = documentFragmentObj.NodeType;
if(nodeType != Node.DOCUMENT_FRAGMENT_NODE)
    printError();
// -->
</script>
</html>

Document.createElement()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

documentObj.createElement(tagName)

Description

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.

Example

Listing 10.18 uses a Document object to create a new element.

Listing 10.18 Checking Whether an Element Has Children Using the hasChildren() Method of Element

<html>
<script language="JScript">
<!--
var elementObj = documentObj.createElement("foo");
var hasChildren = elementObj.hasChildren();
// -->
</script>
</html>

Document.createElementNS()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

documentObj.CreateElementNS(nameSpaceURIqualifiedName)

Description

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.

Example

Listing 10.19 shows how to add an element to the current document.

Listing 10.19 Adding an Element

<html>
<script language="JScript">
<!--
var myLineItemElement = documentObj.createElementNS
     ("http://foo.com/namespace", "foo:lineItem");
// -->
</script>
</html>

Document.createEntityReference()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

Syntax

documentObj.createEntityReference(name)

Description

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.

Example

Listing 10.20 illustrates using a previously created Document object to create an entity reference.

Listing 10.20 Creating an EntityReference

<html>
<script language="JScript">
<!--
var entityReferenceObj = documentObj.createEntityReference("myRef");
if(entityReferenceObj.hasAttributes())
    handleNoAttributes();
// -->
</script>
</html>

Document.createProcessingInstruction()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

documentObj.createProcessingInstruction(targetdata)

Description

The createProcessingInstruction() method of the Document object creates an instance of a ProcessingInstruction node with DOMStringstarget 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.

Example

Listing 10.21 illustrates adding a CDATA section to a previously created document.

Listing 10.21 Adding a CDATA Section

<html>
<script language="JScript">
<!--
var cdataSectionObj = documentObj.createCDATASection("foo CDATA Section");
// -->
</script>
</html>

Document.createTextNode()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

Syntax

documentObj.createTextNode(data)

Description

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.

Example

Listing 10.22 uses a previously created Document object to create a new text node in the current document.

Listing 10.22 Creating a New Text Node Using the createTextNode()

Method of the Document Object

<html>
<script language="JScript">
<!--
var textObj = documentObj.createTextNode("Some new text.");
var splitText = textObj.splitText(4);
// -->
</script>
</html>

Document.doctype

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

Syntax

documentObj.docType

Description

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.

Example

Listing 10.23 shows how to check the document type declaration of the current document.

Listing 10.23 Checking the docType Property

<html>
<script language="JScript">
<!--
var docTypeObj = documentObj.docType;
if(docTypeObj == null)
    handleHTML();
else
    handleXML();
// -->
</script>
</html>

Document.documentElement

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

documentObj.documentElement

Description

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.

Example

Listing 10.24 illustrates using the Document object to retrieve the root element of the current document.

Listing 10.24 Retrieving the Root Document Element Using the

<html>
<script language="JScript">
<!--
var rootElementObj = documentObj.documentElement;
var hasFooAttribute = rootElementObj.hasAttribute("foo");
// -->
</script>
</html>

Document.getElementById()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

documentObj.getElementById(elementId)

Description

The getElementById() method of the Document object returns the Element whose ID is specified by elementId in this Document.

Example

Listing 10.25 shows how to retrieve an element using its element identifier.

Listing 10.25 Retrieving Element by Identifier

<html>
<script language="JScript">
<!--
var myElement = documentObj.getElementById("12932945");
// -->
</script>
</html>

Document.getElementsByTagName()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

documentObj.getElementsByTagName(tagName)

Description

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.

Example

Listing 10.26 illustrates getting all elements in a previously created Document object and iterating through the list.

Listing 10.26 Getting Elements by Tag Name

<html>
<script language="JScript">
<!--
var elementListObj = documentObj.getElementsByTagName("item");
var i = 0;
while ( i < elementListObj.length) {
    processElementValue(elementListObj.item(i).nodeValue);
}
// -->
</script>
</html>

Document.getElementsByTagNameNS()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

documentObj.getElementByTagNameNS(namespaceURI, localName)

Description

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.

Example

Listing 10.27 shows how to retrieve all elements in a document given a tag name.

Listing 10.27 Getting Elements by 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>

Document.implementation

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

Syntax

documentObj.implementation

Description

The read-only property implementation of the Document object returns the DOMImplementation object associated with this document. This property is simply here for convenience.

Example

Listing 10.28 illustrates using a Document object to return the current DOMImplementation.

Listing 10.28 Using the implementation Property

<html>
<script language="JScript">
<!--
var domImplObj = documentObj.implementation;
var hasHTMLEvents = domImplObj.hasFeature("HTMLEvents", "2.0");
//-->
</script>
</html>

Document.importNode()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

Syntax

documentObj.importNode(importedNodedeep)

Description

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.

Example

Listing 10.29 shows a previously created node being imported into the current document and then made a child of an element.

Listing 10.29 Importing a Node

<html>
<script language="JScript">
<!--
var importedNode = documentObj.importNode(newNode, true);
elementObj.append(importedNode);
// -->
</script>
</html>

DocumentFragment

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

Syntax

Core DOM object.

Description

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.

Example

Listing 10.30 illustrates how a Document object is used to create a new DocumentFragment.

Listing 10.30 Creating a Document Fragment Using the

<html>
<script language="JScript">
<!--
var docFragmentObj = documentObj.createDocumentFragment();
// -->
</script>
</html>

DocumentType

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

Core DOM object.

Description

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.

Table 10.4 Arguments Associated With DocumentType Object

Image

Example

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.

DocumentType.entities

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

documentTypeObj.entities

Description

The read-only property entities of the DocumentType object returns a NamedNodeMap containing external and internal entities declared in the DTD.

Example

Listing 10.31 iterates through a list of entities and prints out their notation names.

Listing 10.31 Iterating Through the entities Property of the DocumentType Object

<html> 
<script language="JScript">
<!--
var entityList = docTypeObj.entities;
var i = 0;
while( i < entityList.length) {
    writeEntityName(entityList.item(i).notationName);
}
// -->
</script>
</html>

DocumentType.internalSubset

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

documentTypeObj.InternalSubset

Description

The read-only property internalSubset of the DocumentType object returns a string representation of the internal subset.

Example

Listing 10.32 retrieves the internal subset from a DocumentType object.

Listing 10.32 Reading the internalSubset Property of the DocumentType Object

<html>
<script language="JScript">
<!--var internalSubset = docTypeObj.internatSubset;
// -->
</script>
</html>

DocumentType.name

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

documentTypeObj.name

Description

The read-only property name of the DocumentType object returns the name of this DTD.

Example

Listing 10.33 illustrates retrieving the Name of a DTD using the name property of the DocumentType object.

Listing 10.33 Name Property of DocumentType

<html>
<script language="JScript">
<!--
var docTypeName = docTypeObj.name;
// -->
</script>
</html>

DocumentType.notations

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

documentTypeObj.notations

Description

The read-only property notations of the DocuementType object returns a NamedNodeMap containing all notations declared in the DTD.

Example

Listing 10.34 iterates through a list of notations and writes out their public identifiers.

Listing 10.34 Reading the notations Property of the DocumentType Object

<html>
<script language="JScript">
<!--
var notationList = docTypeObj.notations;
var i = 0;
while( i < entityList.length) {
    writeEntityName(entityList.item(i).publicId);
}
// -->
</script>
</html>

DocumentType.publicId

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

documentTypeObj.publicId

Description

The read-only property publicId of the DocumentType object returns the public identifier of the external subset.

Example

Listing 10.35 retrieves the public identifier from a DocumentType object.

Listing 10.35 Reading the publicID Property of the DocumentType Object

<html>
<script language="JScript">
<!--
var publicId = docTypeObj.publicId;
// -->
</script>
</html>

DocumentType.systemId

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

Syntax

documentTypeObj.systemId

Description

The read-only property systemId of the DocumentType object returns the system identifier of the external subset.

Example

Listing 10.36 retrieves the system identifier from a DocumentType object.

Listing 10.36 Reading the systemId Property of the DocumentType Object

<html>
<script language="JScript">
<!--
var systemId = docTypeObj.systemId;
// -->
</script>
</html>

DOMException

JavaScript1.5+, JScript5.0+

 

Syntax

Core DOM object.

Description

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.

Table 10.5 Arguments Associated With DOMException Object

Image

Example

Listing 10.37 creates a new DOMException object with code value NAMESPACE_ERR and throws it.

Listing 10.37 Creating a DOMException

<html>
<script language="JScript">
<!--
var domExceptionObj = new DOMException(DOMException.NAMESPACE_ERR);
throw domExceptionObj;
// -->
</script>
</html>

DOMException.code

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

domExceptionObj.code

Description

The code property of the DOMException object returns a number indicating which exception has occurred.

Example

Listing 10.38 illustrates catching a DOMException object and interrogating the code property to handle the appropriate error.

Listing 10.38 Catching a DOMException

<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>

DOMImplementation

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

DOM Core object.

Description

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.

Table 10.6 Arguments Associated with the DOMImplementation Object

Image

Example

Listing 10.39 creates a DOMImplementation object and creates a Document.

Listing 10.39 Use of DOMImplementation

<html>
<script language="JScript">
<!--
var domImplObj = new DOMImplementation();
var documentObj = domImplObj.createDocument
     ("http://foobar.org/schema", "foo:bar", null);
// -->
</script>
</html>

DOMImplementation.createDocument()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

DOMImplementation.CreateDocument(namespaceURIqualifiedNamedoctype)

Description

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 than
http://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

Example

Listing 10.40 uses a DOMImplementation object to create a new Document.

Listing 10.40 Creating a Document Using the createDocument() Method of the DOMImplementation Object

<html>
<script language="JScript">
<!--
var domImplObj = new DOMImplementation();
var documentObj = domImplObj.createDocument
    ("http://foobar.org/schema", "foo:bar", null);
//-->
</script>
</html>

DOMImplementation.createDocumentType()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

DOMImplementation.createDocumentType
(qualifiedName, publicId, systemId)

Description

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.

Example

Listing 10.41 uses a DOMImplementation object to create a new empty XML document type.

Listing 10.41 Creating an Empty Document Type Using the createDocumentType() Method of the DOMImplementation Object

<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>

DOMImplementation.hasFeature()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

DOMImplementation.hasFeature (feature, version)

Description

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.

Example

Listing 10.42 uses a DOMImplementation object to check whether level 2.0 contains support for HTML Events.

Listing 10.42 Using the DOMImplementation.hasFeature() Method

<html>
<script language="JScript">
<!--
var domImplObj = new DOMImplementation();
var hasHTMLEvents = domImplObj.hasFeature("HTMLEvents", "2.0");
// -->
</script>
</html>

Element

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

Core DOM object.

Description

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.

Table 10.7 Arguments Associated With Element Object

Image

Example

Listing 10.43 creates an Element from a Document object.

Listing 10.43 Creating an Element from a Document

<html>
<script language="JScript">
<!--
var newElementObj = documentObj.createElement("item");
// -->
</script>
</html>

Element.getAttribute()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

elementObj.getAttribute(name)

Description

The getAttribute() method of the Element object returns the string value for the attribute name.

Example

Listing 10.44 gets a quantity attribute from an element representing a line item on a purchase order.

Listing 10.44 Getting an Attribute from an Element Using the getAttribute() Method of the Element Object

<html>
<script language="JScript">
<!--
var qtyAttrValue = elementObj.getAttribute("quantity");
// -->
</script>
</html>

Element.getAttributeNode()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

elementObj.getAttributeNode(name)

Description

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.

Example

Listing 10.45 gets an attribute node from an element and sets its value.

Listing 10.45 Getting an Attribute Node Using the getAttributeNode() Method of the Element Object

<html>
<script language="JScript">
<!--
var qtyAttr = elementObj.getAttributeNode("quantity");
qtyAttr.value = "3";
// -->
</script>
</html>

Element.getAttributeNodeNS()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

elementObj.getAttributeNodeNS(namespaceURIlocalName)

Description

The getAttributeNodeNS() method of the Element object returns the Attr value containing the specified namespaceURI and localName.

Example

Listing 10.46 retrieves an attribute node representing a quantity from an element representing a line item.

Listing 10.46 Retrieving an Attribute Node Using the getAttributeNodeNS() Method of the Element Object

<html>
<script language="JScript">
<!--
var qtyAttrNodeObj = invoiceElementObj.getAttributeNodeNS
      ("http://foo.com/namespace", "foo:quantity");
// -->
</script>
</html>

Element.getAttributeNS()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

elementObj.getAttributeNS(namespaceURIlocalName)

Description

The getAttributeNS() method of the Element object returns the string value of the attribute specified by the given namespaceURI and localName.

Example

Listing 10.47 gets an element’s attribute using namespace URI and local name.

Listing 10.47 Getting an Attribute Using Namespace URI Using the getAttributeNS() Method of the Element Object

<html>
<script language="JScript">
<!--
var qtyAttrValue = elementObj.getAttributeNS
        ("http://foo.com/namespace", "foo:quantity");
// -->
</script>
</html>

Element.getElementsByTagName()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

elementObj.getElementsByTagName(name)

Description

The getElementsByTagName() method of the Element object returns a NodeList containing all Elements named name.

Example

Listing 10.48 gets a list of line items from an element representing an invoice.

Listing 10.48 Getting Elements by Tag Name Using the getElementsByTagName() Method of the Element Object

<html>
<script language="JScript">
<!--
var lineItemList = invoiceElementObj.getElementsByTagName("lineItem");
// -->
</script>
</html>

Element.getElementsByTagNameNS()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

elementObj.getElementsByTagNameNS(namespaceURIlocalName)

Description

The getElementsByTageNameNS() method of the Element object returns a NodeList of all descendant Elements with the given namespaceURI and localName.

Example

Listing 10.49 gets a list of line items from an element representing an invoice.

Listing 10.49 Getting Elements by Tag name Using the getElementsByTagNameNS() Method of the Element Object

<html>
<script language="JScript">
<!--
var lineItemList = invoiceElementObj.getElementsByTagNameNS
     ("http://foo.com/namespace", "lineItem");
// -->
</script>
</html>

Element.hasAttribute()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

elementObj.hasAttribute(name)

Description

The hasAttribute() method of the Element object returns true if this element has an attribute named name.

Example

Listing 10.50 determines whether a line item element has an attribute for price set.

Listing 10.50 Checking Whether an Element Has an Attribute Using the hasAttribute() Method of the Element Object

<html>
<script language="JScript">
<!--
if(!lineItemElementObj.hasAttribute("price"))
    lineItemElementObj.setAttribute("price", "3.44");
// -->
</script>
</html>

Element.hasAttributeNS()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

elementObj.hasAttributeNS(namespaceURIlocalName)

Description

The hasAttributeNS() method of the Element object returns True if there exists an attribute of this element that has the specified namespaceURI and localName.

Example

Listing 10.51 determines whether a line item element has an attribute for price set.

Listing 10.51 Checking Whether an Element Has an Attribute Using the hasAttributeNS() Method of the Element Object

<html>
<script language="JScript">
<!--
if(!lineItemElementObj.hasAttributeNS("http://foo.com/namespace", "price"))
    lineItemElementObj.setAttribute("price", "3.44");
// -->
</script>
</html>

Element.removeAttribute()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

elementObj.removeAttribute(name)

Description

The removeAttribute() method of the Element object removes the named attribute specified by the string name from this element.

Example

Listing 10.52 removes the quantity attribute from an element representing a line item.

Listing 10.52 Removing an Attribute Using the removeAttribute() Method of the Element Object

<html>
<script language="JScript">
<!--
elementObj.removeAttribute("quantity");
// -->
</script>
</html>

Element.removeAttributeNode()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

elementObj.removeAttributeNode(oldAttr)

Description

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.

Example

Listing 10.53 Removes an attribute node from an element.

Listing 10.53 Removing an Attribute Node Using the removeAttributeNode() Method of the Element Object

<html>
<script language="JScript">
<!--
elementObj.removeAttributeNode(oldNode);
// -->
</script>
</html>

Element.removeAttributeNS()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

elementObj.removeAttributeNS(namespaceURIlocalName)

Description

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.

Example

Listing 10.54 removes the quantity attribute from an element representing an invoice.

Listing 10.54 Removing an Attribute Using the removeAttributeNS() Method of the Element Object

<html>
<script language="JScript">
<!--
invoiceElementObj.removeAttributeNS
         ("http://foo.com/namespace", "foo:quantity");
// -->
</script>
</html>

Element.setAttribute()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

elementObj.setAttribute(namevalue)

Description

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.

Example

Listing 10.55 sets a quantity attribute for an element that represents a line item.

Listing 10.55 Setting an Attribute for an Element Using the setAttribute() Method of the Element Object

<html>
<script language="JScript">
<!--
elementObj.setAttribute("quantity", "3");
// -->
</script>
</html>

Element.setAttributeNode()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

elementObj.setAttributeNode(newAttr)

Description

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.

Example

Listing 10.56 sets a attribute node representing a quantity for an element representing a line item.

Listing 10.56 Setting an Attribute Node Using the setAttributeNode() Method of the Element Object

<html>
<script language="JScript">
<!--
var qtyAttr = documentObj.createAttribute("quantity");
qtyAttr.value = "3";
elementObj.setAttributeNode(qtyAttr);
// -->
</script>
</html>

Element.setAttributeNodeNS()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

elementObj.setAttributeNodeNS(newAttr)

Description

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.

Example

Listing 10.57 sets a new quantity attribute for an element representing a line item.

Listing 10.57 Setting an Attribute Using the setAttributeNodeNS() Method of the Element Object

<html>
<script language="JScript">
<!--
var qtyAttrObj = documentObj.createAttribute("quantity");
lineItemElementObj.setAttributeNodeNS(qtyAttrObj);
// -->
</script>
</html>

Element.setAttributeNS()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

elementObj.setAttributeNS(namespaceURIqualifiedNamevalue)

Description

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.

Example

Listing 10.58 sets an attribute using its namespace URI.

Listing 10.58 Setting an Attribute Using the setAttributeNS() Method of the Element Object

<html>
<script language="JScript">
<!--
invoiceElementObj.setAttributeNS
    ("http://foo.com/namespace", "foo:quantity", "4");
// -->
</script>
</html>

Element.tagName

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

elementObj.tagName

Description

The read-only property tagName of the Element object returns the name of this element.

Example

Listing 10.59 queries an Element for its tagName.

Listing 10.59 Reading the tagName Property of the Element Object

<html>
<script language="JScript">
<!--
var tagName = elementObj.tagName;
// -->
</script>
</html>

Entity

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

Core DOM object.

Description

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.

Table 10.8 Arguments Associated With Entity Object

Image

Example

An Entity is instantiated using the createEntity() method of the Document object. Entity inherits all methods and properties from the Node object.

Entity.notationName

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

entityObj.notationName

Description

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.

Example

Listing 10.60 retrieves the notation name from a Entity object.

Listing 10.60 Reading the notationName Property of the Entity Object

<html>
<script language="JScript">
<!--
var notationName = entityObj.notationName;
// -->
</script>
</html>

Entity.publicId

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

entityObj.publicId

Description

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.

Example

Listing 10.61 retrieves the public identifier from an Entity object.

Listing 10.61 Reading the publicId Property of the Entity Object

<html>
<script language="JScript">
<!--
var publicId = entityObj.publicId;
// -->
</script>
</html>

Entity.systemId

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

entityObj.systemId

Description

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.

Example

Listing 10.62 retrieves the system identifier from an Entity object.

Listing 10.62 Reading the systemId Property of the Entity Object

<html>
<script language="JScript">
<!--
var systemId = entityObj.systemId;
// -->
</script>
</html>

EntityReference

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

Core DOM object.

Description

An EntityReference object inherits all methods and properties from Node.

Example

An EntityReference is instantiated using the createEntityReference() method from the Document object.

NamedNodeMap

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

Core DOM object

Description

This is a list of DOM nodes with names associated with each. Table 10.9 lists all properties and methods of the NamedNodeMap object.

Table 10.9 Arguments Associated With NamedNodeMap Object

Image

Example

Listing 10.63 retrieves the NamedNodeList attributes property from a HTMLBodyElement object and reads its bgColor property.

Listing 10.63 Getting the attributes property as a NameNodeMap and getting the bgColor property.

<html>
<script language="JScript">
<!--
var bodyAtts = bodyObj.attributes;
var color = bodyAtts.bgColor;
// -->
</script>
</html>

NamedNodeMap.getNamedItem()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

namedNodeMapObj.getNamedItem(name)

Description

The getNamedItem() method of the NamedNodeMap object returns a node specified by name.

Example

Listing 10.64 retrieves an attribute object by name from a NamedNodeMap and processes it.

Listing 10.64 Retrieving an Item by Name Using the getNamedItem() Method of NamedNodeMap

<html>
<script language="JScript">
<!--
var attrList = nodeObj.attributes;
var size = attrList.getNamedItem("size");
// -->
</script>
</html>

NamedNodeMap.getNamedItemNS()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

namedNodeMap.getNamedItemNS(namespaceURIlocalName)

Description

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.

Example

Listing 10.65 retrieves an attribute object by localName and a namespace URI from a

NamedNodeMap and processes it.

Listing 10.65 Retrieving an Item by Name Using the getNamedItemNS() Method of the NamedNodeMap Object

<html>
<script language="JScript">
<!--
var attrList = nodeObj.attributes;
var size = attrList.getNamedItemNS("http://foo.com/namespace", "foo:size");
// -->
</script>
</html>

NamedNodeMap.item()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

namedNodeMapObj.item(index)

Description

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.

Example

Listing 10.66 iterates through an attribute list processing each one.

Listing 10.66 Pulling an Item at a Certain Index Using the item() Method of the NamedNodeMap Object

<html>
<script language="JScript">
<!--
var attrList = nodeObj.attributes;
var i = 0;
while(i < attrList.length) {
    processAttr(attrList.item(i));
    i++;
}
// -->
</script>
</html>

NamedNodeMap.length

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

namedNodeMapObj.length

Description

The length property of the NamedNodeMap object returns the number of nodes contained in this named node map.

Example

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.

Listing 10.67 Testing Against NamedNodeMap length

<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>

NamedNodeMap.removeNamedItem()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

   namedNodeMapObj.removeNamedItem(name)

Description

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.

Example

Listing 10.68 removes a named item from an attribute list.

Listing 10.68 Removing a Named Item Using the removeNamedItem() Method of the NamedNodeMap Object

<html>
<script language="JScript">
<!--
attrList.removeNamedItem("size");
// -->
</script>
</html>

NamedNodeMap.removeNamedItemNS()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

   namedNodeMapObj.removeNamedItemNS(name)

Description

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.

Example

Listing 10.69 removes an item from an attribute list.

Listing 10.69 Removing an Item Using the removeNamedItem() Method of the NamedNodeMap Object

<html>
<script language="JScript">
<!--
attrListObj.removeNamedItemNS("size");
// -->
</script>
</html>

NamedNodeMap.setNamedItem()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

nameNodeMapObj.setNamedItem(node)

Description

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

Example

Listing 10.70 sets a new named attribute in an attribute list.

Listing 10.70 Adding a New Named Attribute to an Attribute List Using the setNamedItem() Method of NamedNodeMap

<html>
<script language="JScript">
<!--
var newAttr = documentObj.createAttribute("size");
attrList.setNamedItem(newAttr);
// -->
</script>
</html>

NamedNodeMap.setNamedItemNS()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

   namedNodeMapObj.setNamedItemNS(node)

Description

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

Example

Listing 10.71 sets a new named attribute in an attribute list.

Listing 10.71 Adding a New Named Attribute to an Attribute List Using the setNamedItemNS() Method of the NamedNodeMap Object

<html>
<script language="JScript">
<!--
var newAttr = documentObj.createAttribute("size");
attrList.setNamedItemNS(newAttr);
// -->
</script>
</html>

Node

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

Core DOM object.

Description

The Node object represents a node of an XML document. Table 10.10 lists all constants, properties, and methods of the Node object.

Table 10.10 Arguments Associated With Node Object

Image

Image

Image

Example

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.

Node.attributes

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

   nodeObj.attributes

Description

The read-only property attributes of the Node object returns a NamedNodeMap containing all attributes of this node.

Example

Listing 10.72 iterates through a list of node attributes and processes them.

Listing 10.72 Iterating Through the attributes Property of the Node Object

<html>
<script language="JScript">
<!--
var attrList = nodeObj.attributes;
var i = 0;
while(i < attrList.length) {
       processAttr(attrList.item(i);
       i++;
}
// -->
</script>
</html>

Node.appendChild()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

   nodeObj.appendChild(newChild)

Description

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.

Example

Listing 10.73 adds a new child to a node.

Listing 10.73 Appending a Child Using the appendChild() Method of the Node Object

<html>
<script language="JScript">
<!--
var newElementObj = documentObj.createElement("item");
nodeObj.appendChild(newElementObj);
// -->
</script>
</html>

Node.childNodes

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

   nodeObj.childNodes

Description

The childNodes attribute of the Node object returns a NodeList containing all children of this node.

Example

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.

Listing 10.74 Iterating Through the childNodes Property of the Node Object

<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>

Node.cloneNode()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

   nodeObj.cloneNode(deep)

Description

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.

Example

Listing 10.75 clones a node and then adds the clone as a child to another node.

Listing 10.75 Cloning a Node Using the cloneNode() Method of the Node Object

<html>
<script language="JScript">
<!--
var clonedNodeObj = nodeObj.cloneNode(true);
anotherNodeObj.appendChild(clonedNodeObj);
// -->
</script>
</html>

Node.firstChild

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

   nodeObj.firstChild

Description

The read-only property firstChild of the Node object returns the first child of this node.

Example

Listing 10.76 checks to see the type of the first child of this node.

Listing 10.76 Reading the firstChild Property of the Node Object

<html>
<script language="JScript">
<!--
var childNodeObj = nodeObj.firstChild;
if(childNodeObj.nodeName == "foo")
       process(childNodeObj);
// -->
</script>
</html>

Node.hasAttributes()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

   nodeObj.hasAttributes()

Description

The hasAttributes() method of the Node object returns True if this node has any attributes attached to it.

Example

Listing 10.77 checks to see whether a node has any attributes and iterates the list if it does.

Listing 10.77 Checking for Attributes Using the hasAttributes() Method of the Node Object

<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>

Node.hasChildNodes()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

   nodeObj.hasChildNodes()

Description

The hasChildNodes() method of the Node object returns true if this node has children.

Example

Listing 10.78 tests to see if a node has children.

Listing 10.78 Checking for children using the hasChildNodes() method of the Node object

<html>
<script language="JScript">
<!--
if(nodeObj.hasChildNodes())
     handleChildren(nodeObj);
// -->
</script>
</html>

Node.insertBefore()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

   nodeObj.insertBefore(newChildrefChild)

Description

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

Example

Listing 10.79 inserts a new child node before a reference child node.

Listing 10.79 Inserting a New Child Node Using the insertBefore() Method of the Node Object

<html>
<script language="JScript">
<!--
var newElementObj = documentObj.createElement("item");
nodeObj.insertBefore(newElementObj, existingChild);
// -->
</script>
</html>

Node.isSupported()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

   nodeObj.isSupported(featureversion)

Description

The isSupported() method of the Node object returns whether the specified feature and version are supported by this node.

Example

Listing 10.80 queries a node for the availability of a certain feature.

Listing 10.80 Checking Whether a Feature Is Supported Using the isSupported() Method of the Node Object

<html>
<script language="JScript">
<!--
if(nodeObj.isSupported("HTMLEvents", "1.0")
     handleHTMLEvents();
// -->
</script>
</html>

Node.lastChild

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

   nodeObj.lastChild

Description

The read-only property lastChild of the Node object returns the last child of this node.

Example

Listing 10.81 checks to see the type of the last child of this node.

Listing 10.81 Reading the lastChild Property of the Node Object

<html>
<script language="JScript">
<!--
var childNodeObj = nodeObj.lastChild;
if(childNodeObj.nodeName == "foo")
    process(childNodeObj);
// -->
</script>
</html>

Node.localName

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

   nodeObj.localName

Description

The read-only property localName of the Node object returns the local part of the qualified name for this node.

Example

Listing 10.82 takes the local name of a node and prepends the prefix to give the qualified name of the node.

Listing 10.82 Assigning the localName Property of the Node Object

<html>
<script language="JScript">
<!--
var localName = nodeObj.localName;
var prefix = nodeObj.prefix;
var qualifiedName = prefix+":"+localName;
// -->
</script>
</html>

Node.namespaceURI

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

   nodeObj.namespaceURI

Description

The read-only property namespaceURI of the Node object returns a string containing the namespace URI for this node.

Example

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.

Listing 10.83 Reading the namespaceURI Property of the Node Object

<html>
<script language="JScript">
<!--
if((nodeObj.Prefix == "foo" )
     && (nodeObj.NamespaceURI != "http://foo.com/namespace"))
     throw new DOMException(DOMException.NAMESPACE_ERR);
// -->
</script>
</html>

Node.nextSibling

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

   nodeObj.nextSibling

Description

The read-only property nextSibling of the Node object returns the node immediately after this node.

Example

Listing 10.84 checks to see the type of the next sibling of this node.

Listing 10.84 Reading the nextSibling Property of the Node Object

<html>
<script language="JScript">
<!--
var siblingNodeObj = nodeObj.nextSibling;
if(siblingNodeObj.nodeName == "foo")
    process(siblingNodeObj);
// -->
</script>
</html>

Node.nodeName

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

   nodeObj.nodeName

Description

The nodeName property of the Node object returns the name of this node as indicated by the nodeType attribute.

Example

Listing 10.85 takes a Node and performs a particular operation depending on the nodeName.

Listing 10.85 Checking the nodeName Property

<html>
<script language="JScript">
<!--
if(elementObj.nodeName == "foobar")
    processElement(elementObj);
// -->
</script>
</html>

Node.nodeType

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

   nodeObj.nodeType

Description

The nodeType property of the Node object returns a number indicating the node type of this node as represented by the constants listed previously.

Example

Listing 10.86 checks the type of a node and processes it accordingly.

Listing 10.86 Checking the nodeType Property of the Node Object

<html>
<script language="JScript">
<!--
var nodeType = textObj.nodeType;
if(nodeType == Node.TEXT_NODE)
    handleText(textObj);
// -->
</script>
</html>

Node.nodeValue

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

   nodeObj.nodeValue

Description

The nodeValue property of the Node object returns the value of this node depending on its nodeType attribute

Example

Listing 10.87 illutstrates iterating through a list of elements processing each object’s nodeValue property.

Listing 10.87 Processing the nodeValue Property of the Node Object

<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>

Node.normalize()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

   nodeObj.normalize()

Description

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.

Example

Listing 10.88 demonstrates normalizing a node before saving it to a file.

Listing 10.88 Normalizing a Node Using the normalize() Method of the Node Object

<html>
<script language="JScript">
<!--
nodeObj.normalize();
writeNode(nodeObj);
// -->
</script>
</html>

Node.ownerDocument

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

   nodeObj.ownerDocument

Description

The read-only property ownerDocument of the Node object returns the Document object associated with this node.

Example

Listing 10.89 retrieves the owner document of this node and creates a new element.

Listing 10.89 Reading the ownerDocument Property of the Node Object

<html>
<script language="JScript">
<!--
var documentObj = nodeObj.ownerDocument;
var elementObj = documentObj.createElement("lineItem");
nodeObj.appendChild(elementObj);
// -->
</script>
</html>

Node.parentNode

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

   nodeObj.parentNode

Description

The parentNode property of the Node object returns the parent of this node.

Example

Listing 10.90 checks to see whether a node object has a parent.

Listing 10.90 Checking the parentNode Property of the Node Object

<html>
<script language="JScript">
<!--
if(elementObj.parentNode != null) 
    processAsChild(elementObj);
// -->
</script>
</html>

Node.prefix

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

   nodeObj.prefix

Description

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

Example

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.

Listing 10.91 Reading the prefix Property of the Node Object

<html>
<script language="JScript">
<!--
if((nodeObj.prefix == "foo")
      && (nodeObj.namespaceURI != "http://foo.com/namespace"))
    throw new DOMException(DOMException.NAMESPACE_ERR);
// -->
</script>
</html>

Node.previousSibling

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

   nodeObj.previousSibling

Description

The read-only property previousSibling of the Node object returns the node immediately before this node.

Example

Listing 10.92 illustrates checking to see the type of the previous sibling of this node.

Listing 10.92 Reading the previousSibling Property of the Node Object

<html>
<script language="JScript">
<!--
var siblingNodeObj = nodeObj.previousSibling;
if(siblingNodeObj.nodeName == "foo")
    process(siblingNodeObj);
// -->
</script>
</html>

Node.removeChild()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

   nodeObj.removeChild(oldChild)

Description

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.

Example

Listing 10.93 removes a child from a node.

Listing 10.93 Removing a Child from a Node Using the removeChild() Method of the Node Object

<html>
<script language="JScript">
<!--
nodeObj.removeChild(existingChild);
// -->
</script>
</html>

Node.replaceChild()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

   nodeObj.replaceChild(newChildoldChild)

Description

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

Example

Listing 10.94 replaces an existing child node with a new node.

Listing 10.94 Replacing a Child Node Using the replaceChild() Method of the Node Object

<html>
<script language="JScript">
<!--
var newElementObj = documentObj.createElement("item");
nodeObj.replaceChild(newElementObj, existingChild);
// -->
</script>
</html>

Notation

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

Core DOM object.

Description

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.

Table 10.11 Arguments Associated With Notation Object

Image

Example

A Notation is instantiated using the createNotation() method of the Document object.

Notation.publicId

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

   notationObj.publicId

Description

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.

Example

Listing 10.95 retrieves the public identifier from a Notation object.

Listing 10.95 Reading the publicId Property of the Notation Object

<html>
<script language="JScript">
<!--
var publicId = docTypeObj.publicId;
// -->
</script>
</html>

Notation.systemId

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

   notationObj.systemId

Description

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.

Example

Listing 10.96 retrieves the system identifier from a Notation object.

Listing 10.96 Reading the systemId Property of the Notation Object

<html>
<script language="JScript">
<!--
var systemId = docTypeObj.systemId;
// -->
</script>
</html>

ProcessingInstruction

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

Core DOM object.

Description

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.

Table 10.12 Arguments Associated With ProcessingInstruction Object

Image

Example

A ProcessingInstruction is instantiated using the createProcessingInstruction() method of the Document object.

ProcessingInstruction.data

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

   processingInstructionObj.data

Description

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.

Example

Listing 10.97 illustrates the retrieval of the data property.

Listing 10.97 Reading the data Property of the ProcessingInstruction Object

<html>
<script language="JScript">
<!--
var instructionData= processingInstructionObj.data;
// -->
</script>
</html>

ProcessingInstruction.target

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

   processingInstructionObj.target

Description

The readonly property target if the ProcessingInstruction object returns the target of this processing instruction.

Example

Listing 10.97 illustrates the retrieval of the target property.

Listing 10.97 Reading the target Property of the

<html>
<script language="JScript">
<!--
var targetToken = processingInstructionObj.target;
// -->
</script>
</html>

Text

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

Core DOM object.

Description

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:

Image

Example

A Text object is created by the createTextNode() method of the Document object.

Text.splitText()

JavaScript 1.5+, JScript 5.0+

Nav6+, IE5+

 

Syntax

   textObj.splitText(offset)

Description

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.

Example

Listing 10.98 demonstrates splitting a Text node’s Data into another Text node.

Listing 10.98 Splitting Text Using the splitText() Method of the Text Object

<html>
<script language="JScript">
<!--
var textObj = documentObject.createTextNode("This is some text");
var splitTextObj = textObj.splitText(5);
// -->
</script>
</html>

ON THE CD-ROM

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.37.254