15.02.2013 Views

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

294<br />

<strong>JavaScript</strong> <strong>Examples</strong> <strong>Bible</strong>: The Essential Companion to <strong>JavaScript</strong> <strong>Bible</strong><br />

Methods<br />

appendData(“text”)<br />

deleteData(offset, count)<br />

insertData(offset, “text”)<br />

replaceData(offset, count, “text”)<br />

substringData(offset, count)<br />

TextNode.appendData()<br />

NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5<br />

Compatibility ✓<br />

Example<br />

The page created by Listing 19-8 is a working laboratory that you can use to<br />

experiment with the five data-related methods in NN6+. The text node that invokes<br />

the methods is a simple sentence in a P element. Each method has its own clickable<br />

button, followed by two or three text boxes into which you enter values for method<br />

parameters. Don’t be put off by the length of the listing. Each method’s operation is<br />

confined to its own function and is fairly simple.<br />

Each of the data-related methods throws exceptions of different kinds. To help<br />

handle these errors gracefully, the method calls are wrapped inside a try/catch<br />

construction. All caught exceptions are routed to the handleError() function<br />

where details of the error are inspected and friendly alert messages are displayed<br />

to the user. See Chapter 39 of the <strong>JavaScript</strong> <strong>Bible</strong> for details on the try/catch<br />

approach to error handling in W3C DOM-capable browsers.<br />

Listing 19-8: Text object Data Method Laboratory<br />

<br />

<br />

Data Methods of a W3C Text Object<br />

<br />

function doAppend(form) {<br />

var node = document.getElementById(“myP”).firstChild<br />

var newString = form.appendStr.value<br />

try {<br />

node.appendData(newString)<br />

}<br />

catch(err) {<br />

handleError(err)<br />

}<br />

}<br />

function doDelete(form) {<br />

var node = document.getElementById(“myP”).firstChild<br />

var offset = form.deleteOffset.value

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!