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.

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

Example<br />

Use The Evaluator to experiment with the normalize() method in NN6. The following<br />

sequence adds a text node adjacent to one in the myP element. A subsequent<br />

invocation of the normalize() method removes the division between the adjacent<br />

text nodes.<br />

Begin by confirming the number of child nodes of the myP element:<br />

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

Three nodes initially inhabit the element. Next, create a text node and append it as<br />

the last child of the myP element:<br />

a = document.createTextNode(“This means you!”)<br />

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

With the new text now rendered on the page, the number of child nodes<br />

increases to four:<br />

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

You can see that the last child node of myP is the text node you just created:<br />

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

But by invoking normalize() on myP, all adjacent text nodes are accumulated into<br />

single nodes:<br />

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

You can now see that the myP element is back to three child nodes, and the last<br />

child is a combination of the two previously distinct, but adjacent, text nodes:<br />

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

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

releaseCapture()<br />

setCapture(containerBoolean)<br />

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

Compatibility ✓ ✓<br />

Example<br />

Listing 15-30 demonstrates the usage of setCapture() and releaseCapture()<br />

in a “quick-and-dirty” context menu for IE5+/Windows. The job of the context menu<br />

is to present a list of numbering styles for the ordered list of items on the page.<br />

Whenever the user brings up the context menu atop the OL element, the custom<br />

context menu appears. Event capture is turned on in the process to prevent mouse<br />

actions elsewhere on the page from interrupting the context menu choice. Even a<br />

click on the link set up as the title of the list is inhibited while the context menu is<br />

visible. A click anywhere outside of the context menu hides the menu. Clicking a<br />

elementObject.releaseCapture()

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

Saved successfully!

Ooh no, something went wrong!