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.

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

Example<br />

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

method in action. Begin by verifying that the myEM element has but one child node,<br />

and that its nodeValue is the string “all”:<br />

document.getElementById(“myEM”).childNodes.length<br />

document.getElementById(“myEM”).firstChild.nodeValue<br />

Next, split the text node into two pieces after the first character:<br />

document.getElementById(“myEM”).firstChild.splitText(1)<br />

Two text nodes are now inside the element:<br />

document.getElementById(“myEM”).childNodes.length<br />

Each text node contains its respective portion of the original text:<br />

document.getElementById(“myEM”).firstChild.nodeValue<br />

document.getElementById(“myEM”).lastChild.nodeValue<br />

If you are using NN6, now bring the text nodes back together:<br />

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

document.getElementById(“myEM”).childNodes.length<br />

At no time during these statement executions does the rendered text change.<br />

TextRange Object<br />

Properties<br />

boundingHeight<br />

boundingLeft<br />

boundingTop<br />

boundingWidth<br />

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

Compatibility ✓ ✓ ✓<br />

Example<br />

Listing 19-9 provides a simple playground to explore the four bounding properties<br />

(and two offset properties) of a TextRange object. As you select text in the big<br />

paragraph, the values of all six properties are displayed in the table. Values are also<br />

updated if you resize the window via an onResize event handler.<br />

Notice, for example, if you simply click in the paragraph without dragging a<br />

selection, the boundingWidth property shows up as zero. This action is the equivalent<br />

of a TextRange acting as an insertion point.<br />

297<br />

TextRange.boundingHeight

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

Saved successfully!

Ooh no, something went wrong!