15.02.2013 Views

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Methods<br />

cloneContents()<br />

cloneRange()<br />

Chapter 5 ✦ Body Text Objects (Chapter 19)<br />

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

Compatibility ✓<br />

Example<br />

When Netscape outfits the NN6 browser with the cloneContents() method, use<br />

The Evaluator (Chapter 13 in the <strong>JavaScript</strong> <strong>Bible</strong>) to see the method in action.<br />

Begin by creating a new range object that contains the text of the myP paragraph<br />

element.<br />

a = document.createRange()<br />

a.selectNode(document.getElementById(“myP”)<br />

Next, clone the original range and preserve the copy in variable b:<br />

b = a.cloneContents()<br />

Move the original range so that it is an insertion point at the end of the body by<br />

first expanding it to encompass the entire body and then collapse it to the end<br />

a.selectNode(document.body)<br />

a.collapse(false)<br />

Now, insert the copy at the very end of the body:<br />

a.insertNode(b)<br />

If you scroll to the bottom of the page, you see a copy of the text.<br />

See the description of the compareBoundaryPoints() method later in this<br />

chapter to see an example of the cloneRange() method.<br />

collapse([startBoolean])<br />

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

Compatibility ✓<br />

Example<br />

See Listings 19-11 (in this chapter) and 15-14 (in Chapter 1 of this book) to see<br />

the collapse() method at work (albeit with the IE TextRange object).<br />

279<br />

Range.collapse()

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

Saved successfully!

Ooh no, something went wrong!