Chapter 3 DynaScript Predefined Objects


DOMNode object

Object

Represents a single node in the DOM document tree.

Syntax

To use a DOMNode's property:

DOMNode.propertyName

To use a DOMNode's method:

DOMNode.MethodName( parameter ) 

Description

The DOMNode object is the primary object type for the entire Document Object Model (DOM) suite of objects. It is the object from which most of the other DOM objects inherit.

Each object that inherits from the DOMNode exposes a nodeType property to identify the object. See the nodeType property below for valid values for the nodeType.

All objects inheriting from the DOMNode object expose methods for dealing with children, but not all node types necessarily have children. For example, text nodes cannot have children. See the childNodes property below for a list of which types of nodes can have children.

While each object that inherits from DOMNode exposes an attributes property, only DOMElement objects can have attributes.

attributes property

Syntax

DOMNode.attributes

Description

A DOMNamedNodeMap containing the attributes of this node. This property is applicable only to nodes that are elements.

Example

This fragment tests to see if a node is an element, and assigns the attributes of the element to a DOMNamedNodeMap named attlist.

if( node.nodeType == 1 ){ // Element
  attlist = node.attributes
} else {
  document.writeln( "Not an element" );
}

See also

"DOMNamedNodeMap object"

childNodes property

Syntax

DOMNode.childNodes

Description

A DOMNodeList object that contains all the children of this node. If there are no children, then this is a node list containing zero nodes.

Note  

Performance tip
To loop over a childNodes list, it is more efficient to use the nextSibling property than to explicitly use the childNodes list of the parent object.

Not all nodes can have children. This table lists the types of children that are valid for each type of node.

Node type Node types valid for children
document element (maximum of one), processing instruction, comment, document type
document fragment element, processing instruction, comment, text, CDATA section, entity reference
document type no children
entity reference element, processing instruction, comment, text, CDATA section, entity reference
element element, text, comment, processing instruction, CDATA section, entity reference
attribute text, entity reference
processing instruction no children
comment no children
text no children
CDATA section no children
entity element, processing instruction, comment, text, CDATA section, entity reference
notation no children

Example

This example loops through the childNodes of a node:

function ChildNodes( domDoc ){
  docElem = domDoc.documentElement;
  var node = docElem.firstChild;
  do {
    document.writeln( node.nodeName );
    node = node.nextSibling;
  } while ( node != null )
}

firstChild property

Syntax

DOMNode.firstChild

Description

The DOMNode object representing the first child of this node, or null if there are no children.

Example

This fragment sets the variable node to the first child of the document element:

var docElem = domDoc.documentElement;
var node = docElem.firstChild;

lastChild property

Syntax

DOMNode.lastChild

Description

The DOMNode object representing the last child of this node, or null if there are no children.

Example

This fragment sets the variable node to the last child of the document element:

var docElem = domDoc.documentElement;
var node = docElem.lastChild;

nextSibling property

Syntax

DOMNode.nextSibling

Description

The DOMNode object representing the node immediately following this node, or null if this is the last node.

Example

This fragment loops over the siblings of an element named node:

do {
  document.writeln( node.nodeName );
  node = node.nextSibling;
} while ( node != null )

nodeName property

Syntax

DOMNode.nodeName

Description

The name of this node. The name of the node depends on the node type. This table lists the contents of the nodeName property for each node type:

Node type Contents of nodeName property
document "document"
document fragment "document-fragment"
document type document type name
entity reference name of entity referenced
element tag name
attribute name of attribute
processing instruction target
comment "comment"
text "text"
CDATA section "cdata-section"
entity entity name
notation notation name

XML is case sensitive. If you check the name of a node in a comparison, you must ensure the check is case sensitive.

Example

This fragment writes out the name of each of a set of nodes, held in a variable named node:

do {
  document.writeln( node.nodeName );
  node = node.nextSibling;
} while ( node != null )

See also

"getElementsByTagName method"

nodeType property

Syntax

DOMNode.nodeType

Description

An integer representing the type of node object. This table lists the nodeType number for each type of node:

Node type Contents of node type property
element 1
attribute 2
text 3
CDATA section 4
entity reference 5
entity 6
processing instruction 7
comment 8
document 9
document type 10
document fragment 11
notation 12

Example

This fragment performs different operations based on the type of node. The node is held in a variable named domNode.

switch domNode.nodeType {
  case 1: 
    //element operations
    break;
  case 2: 
    //attribute operations
    break;
  case 3: 
    //text operations
    break;
  case 4: 
    //CDATA section     operations
    break;
  case 5: 
    //entity reference     operations
    break;
  case 6: 
    //entity     operations
    break;
  case 7: 
    // processing instruction     operations
    break;
  case 8: 
    //comment     operations
    break;
  case 9: 
    //document     operations
    break;
  case 10: 
    // document type     operations
    break;
  case 11: 
    // document fragment     operations
    break;
  case 12: 
    //notation     operations
    break;
  default:
    document.writeln( "Invalid node type: " +  
               domChild.nodeType );
  }

nodeValue property

Syntax

DOMNode.nodeValue

Description

The value of this node. The value depends on the type of node. This table lists the contents of the nodeValue property for each node type:

Node type Contents of nodeValue property
document null
document fragment null
document type null
entity reference null
element null
attribute value of attribute
processing instruction entire content excluding the target
comment content of the comment
text content of the text node
CDATA section content of the CDATA section
entity null
notation null

Example

This fragment writes out the nodeValue for an attribute named Type:

if( domAtt.nodeName == "Type" ){
  document.writeln( domAtt.nodeValue );
}

ownerDocument property

Syntax

DOMNode.ownerDocument

Description

The DOMDocument object associated with this node. For DOMDocument objects, this property is null.

Example

This fragment writes out the name of the document element for the DOMDocument object that owns domNode.

var domDoc = domNode.ownerDocument;
document.writeln( domDoc.documentElement.nodeName );

See also

"DOMDocument object"

parentNode property

Syntax

DOMNode.parentNode

Description

The DOMNode object representing the parent of this node, or null if there is no parent. This node objects do not have a parent: DOMDocument , DOMDocumentFragment , DOMAttribute, DOMEntity, DOMDocumentType, and DOMNotation .

Example

This example writes out the parent of the node held in the variable domNode, if it exists:

if( domNode.parentNode != null ){
  document.writeln( domNode.parentNode.nodeName;
}

previousSibling property

Syntax

DOMNode.previousSibling

Description

The DOMNode object representing the node immediately preceding this node, or null if no nodes precede this node.

Example

This function loops backwards through the children of a node.

function BackwardsChildNodes( domNode ){
  var domChild = domNode.lastChild;
  do {
    document.writeln( domChild.nodeName ); 
    domChild = domChild.previousSibling;
  } while ( domChild != null )
}

appendChild method

Syntax

DOMNode.appendChild( newChild )

Description

Adds the DOMNode newChild to the end of the list of children of this node. If newChild already exists in the tree, it is first removed.

Return

The DOMNode which was added or null if the node could not be appended (for example, if newChild is not a valid type of child for this node)

Example

This fragment adds a child element to a node held in the customer variable.

new_elem = domDoc.createElement( "Name" );
new_text = domDoc.createTextNode("Ann T. Dote" );
el = customer.appendChild( new_elem );
el.appendChild( new_text );

cloneNode method

Syntax

DOMNode.cloneNode( deep )

Description

Returns a duplicate of this DOMNode . The duplicate node has no parent. deep is a Boolean indicating whether to clone the subtree under this node.

For DOMElement objects, all attributes and their values are copied.

Return

The duplicate DOMNode

Example

This fragment makes a duplicate of the first child of a document element (and its subtree) and adds it to the end of the list of its children. The call to prettyPrint is for debugging purposes.

var docElem = domDoc.documentElement;
var thisNode = docElem.firstChild;
var newNode = thisNode.cloneNode( true );
docElem.appendChild( newNode );
document.writeln( domDoc.prettyPrint() ); 

hasChildNodes method

Syntax

DOMNode.hasChildNodes( )

Description

Indicates whether a node has any children.

Return

Boolean.

Example

This statement tests to see whether thisNode has any children:

if( thisNode.hasChildNodes() ){
  // operations here
}

insertBefore method

Syntax

DOMNode.insertBefore( newChild, refChild )

Description

Inserts the DOMNode newChild before the existing child DOMNode refChild. If refChild is null, newChild is inserted at the end of the list of children.

If newChild is a DOMDocumentFragment , all of its children are inserted, in order, before refChild. If newChild already exists in the tree, it is first removed.

Return

The inserted DOMNode , or null if the node could not be inserted (for example, if refChild is not a child of this node)

Example

This fragment makes a duplicate of the first child of a document element (and its subtree) and adds it as the penultimate child. The call to prettyPrint is for debugging purposes.

var docElem = domDoc.documentElement;
var thisNode = docElem.firstChild;
var newNode = thisNode.cloneNode( true );
docElem.insertBefore( newNode, docElem.lastChild );
document.writeln( domDoc.prettyPrint() ); 

removeChild method

Syntax

DOMNode.removeChild( oldChild )

Description

Removes from the list of children, the child node indicated by oldChild.

Return

The removed DOMNode or null if the child could not be removed (for example, if oldChild is not a child of this node)

Example

This fragment removes the last child of the document element from a document:

var docElem = domDoc.documentElement;
var thisNode = docElem.lastChild;
var oldNode = docElem.removeChild( thisNode );

replaceChild method

Syntax

DOMNode.replaceChild( newChild, oldChild )

Description

Replaces the child DOMNode oldChild with the DOMNode newChild in the list of children. If newChild already exists in the tree, it is first removed.

Return

The replaced DOMNode (oldChild) or null if the node could not be replaced (for example, if oldChild is not a child of this node)

Example

This fragment replaces the last child of the document element with the first child. The first child is removed from its place in the beginning of the list.

var thisNode = docElem.firstChild;
var oldNode = docElem.replaceChild( thisNode, 
                        docElem.lastChild );

 


Copyright © 2001 Sybase, Inc. All rights reserved.