Chapter 3 DynaScript Predefined Objects
The DOMCharacterData
object inherits
the methods from the DOMNode
object, but
it also has its own methods, which are discussed in this section.
DOMCharacterData.appendData( DOMString )
Appends the string to the end of the character data represented by this object. DOMString is the string that is to be appended.
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 );
DOMCharacterData.deleteData( offset, count )
Removes a range of characters from the text represented by this object. The parameters are:
to
the end of the data are deleted.
This DynaScript statement deletes the first two characters from a text node.
if( elem.nodeType == 3 ) { // text node
elem.deleteData( 0, 2 )
}
DOMCharacterData.insertData( offset, DOMString )
Inserts a string at the specified character offset. The parameters are:
This DynaScript fragment inserts a string at the beginning of a text node.
if( elem.nodeType == 3 ) { // text node
elem.insertData( 0, "Region: " )
}
DOMCharacterData.replaceData( offset, count, DOMString )
Replaces the character starting at the specified character offset with the specified string. The parameters are:
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" )
}
DOMCharacterData.substringData( offset, count )
Extracts a portion of the text represented by this object. The parameters are:
A string containing the extracted portion of the text.
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. |