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.

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

<br />

EndToStart<br />

&nbsp;<br />

End of Fixed<br />

vs. Start of Selection<br />

<br />

<br />

EndToEnd<br />

&nbsp;<br />

End of Fixed<br />

vs. End of Selection<br />

<br />

<br />

<br />

<br />

Lorem ipsum dolor sit, consectetaur adipisicing<br />

elit,<br />

sed do eiusmod tempor incididunt ut labore et dolore aliqua. Ut enim adminim<br />

veniam,<br />

quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo<br />

consequat.<br />

<br />

<br />

duplicate()<br />

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

Compatibility ✓ ✓ ✓<br />

Example<br />

Use The Evaluator (Chapter 13 in the <strong>JavaScript</strong> <strong>Bible</strong>) to see how the duplicate()<br />

method works. Begin by creating a new TextRange object that contains the text of the<br />

myP paragraph element.<br />

a = document.body.createTextRange()<br />

a.moveToElementText(myP)<br />

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

b = a.duplicate()<br />

The method returns no value, so don’t be alarmed by the “undefined” that appears<br />

in the Results box. Move the original range so that it is an insertion point at the end<br />

of the body by first expanding it to encompass the entire body, and then collapse it<br />

to the end:<br />

a.expand(“textedit”)<br />

a.collapse(false)<br />

303<br />

TextRange.duplicate()

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

Saved successfully!

Ooh no, something went wrong!