Chapter 3 DynaScript Predefined Objects


DOMCharacterData methods

The DOMCharacterData object inherits the methods from the DOMNode object, but it also has its own methods, which are discussed in this section.

appendData method

Syntax

DOMCharacterData.appendData( DOMString )

Description

Appends the string to the end of the character data represented by this object. DOMString is the string that is to be appended.

Example

Here is a very simple document:

<?xml version='1.0' encoding='ISO-8859-1' ?>
<Customer>
<FName>Jessie</FName>
<LName>Gagliardo</LName>
<Address><Street>2800 Park Avenue</Street>
<City>Hull</City>
<Region Type="Province">PQ</Region>
</Address></Customer>

This DynaScript fragment, which includes a Region element, checks whether the region is a province or a state, and appends a string indicating the country to the text element.

child = elemRegion.firstChild;
if( elemRegion.getAttribute( "Type" ) == "Province" ){
child.appendData( ", Canada" );
} else {
child.appendData( ", USA" );
}
document.writeln( child.data );

deleteData method

Syntax

DOMCharacterData.deleteData( offset, count )

Description

Removes a range of characters from the text represented by this object. The parameters are:

Example

This DynaScript statement deletes the first two characters from a text node.

if( elem.nodeType == 3 ) { // text node
elem.deleteData( 0, 2 )
}

insertData method

Syntax

DOMCharacterData.insertData( offset, DOMString )

Description

Inserts a string at the specified character offset. The parameters are:

Example

This DynaScript fragment inserts a string at the beginning of a text node.

if( elem.nodeType == 3 ) { // text node
elem.insertData( 0, "Region: " )
}

replaceData method

Syntax

DOMCharacterData.replaceData( offset, count, DOMString )

Description

Replaces the character starting at the specified character offset with the specified string. The parameters are:

Example

This DynaScript fragment replaces the third and fourth characters in a text node with the string "XXX".

if( elem.nodeType == 3 ) { // text node
elem.replaceData( 3, 2, "XXX" )
}

substringData method

Syntax

DOMCharacterData.substringData( offset, count )

Description

Extracts a portion of the text represented by this object. The parameters are:

Return

A string containing the extracted portion of the text.

Example

This DynaScript fragment writes out the characters from the fourth to the hundredth of all text nodes that are children of a Street element.

function SubstringDataCharacterData( domDoc ) {
var elemlist = domDoc.getElementsByTagName( "Street" )
for( iElem=0; iElem < elemlist.length; iElem++ ) {
var elem = elemlist.item( iElem );
var child = elem.firstChild;
do {
if( child.nodeType == 3 ){ // text node
var svar = child.substringData( 3, 99 );
document.writeln( "svar = " + svar );
}
child = child.nextSibling;
} while ( child != null )
}
}

 


Copyright © 1999 Sybase, Inc. All rights reserved.