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.

278<br />

Range.endOffset<br />

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

Now set the range to encompass the myEM element:<br />

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

Inspect the containers for both the start and end points of the selection:<br />

a.startContainer.id<br />

a.endContainer.id<br />

The range encompasses the entire myEM element, so the start and end points are<br />

outside of the element. Therefore, the container of both start and end points is the<br />

myP element that also surrounds the myEM element.<br />

endOffset<br />

startOffset<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 experiment with the<br />

endOffset and startOffset properties, following similar paths you just saw in<br />

the description. Reload the page and assign a new range to the a global variable by<br />

typing the following statement into the top text box:<br />

a = document.createRange()<br />

Now set the range to encompass the myEM element and then move the start point<br />

outward to a character within the myP element’s text node:<br />

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

a.setStart(document.getElementById(“myP”).firstChild, 7)<br />

Inspect the node types of the containers for both the start and end points of the<br />

selection:<br />

a.startContainer.nodeType<br />

a.endContainer.nodeType<br />

The startContainer node type is 3 (text node), while the endContainer node<br />

type is 1 (element). Now inspect the offsets for both the start and end points of the<br />

selection:<br />

a.startOffset<br />

a.endOffset

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

Saved successfully!

Ooh no, something went wrong!