04.11.2015 Views

javascript

Create successful ePaper yourself

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

Chapter 11: DOM Levels 2 and 3<br />

The TreeWalker type also has a property called currentNode that indicates the node that was last<br />

returned from the traversal via any of the traversal methods. This property can also be set to change<br />

where the traversal continues from when it resumes, as shown in this example:<br />

var node = walker.nextNode();<br />

alert(node === walker.currentNode); //true<br />

walker.currentNode = document.body; //change where to start from<br />

Compared to NodeIterator , the TreeWalker type allows greater flexibility when traversing the DOM.<br />

There is no equivalent in IE, so cross - browser solutions using traversals are quite rare.<br />

Ranges<br />

To allow an even greater measure of control over a page, the DOM Level 2 Traversal and Range module<br />

defines an interface called a range . A range can be used to select a section of a document regardless of node<br />

boundaries (this selection occurs behind the scenes and cannot be seen by the user). Ranges are helpful<br />

when regular DOM manipulation isn ’ t specific enough to change a document. DOM ranges are supported<br />

in Firefox, Opera, Safari, and Chrome. IE implements ranges in a proprietary way.<br />

Ranges in the DOM<br />

DOM Level 2 defines a method on the Document type called createRange() . In DOM - compliant<br />

browsers, this method belongs to the document object. You can test for the range support by using<br />

hasFeature() or by checking for the method directly. Here is an example:<br />

var supportsRange = document.implementation.hasFeature(“Range”, “2.0”);<br />

var alsoSupportsRange = (typeof document.createRange == “function”);<br />

If the browser supports it, a DOM range can be created using createRange() , as shown here:<br />

var range = document.createRange();<br />

Similar to nodes, the newly created range is tied directly to the document on which it was created and cannot<br />

be used on other documents. This range can then be used to select specific parts of the document behind the<br />

scenes. Once a range has been created and its position set, a number of different operations can be performed<br />

on the contents of the range, allowing more fine - grained manipulation of the underlying DOM tree.<br />

Each range is represented by an instance of the Range type, which has a number of properties and<br />

methods. The following properties provide information about where the range is located in the<br />

document:<br />

❑<br />

❑<br />

startContainer — The node within which the range starts (the parent of the first node in the<br />

selection).<br />

startOffset — The offset within the startContainer where the range starts. If<br />

startContainer is a text node, comment node, or CData node, the startOffset is the<br />

number of characters skipped before the range starts; otherwise, the offset is the index of the<br />

first child node in the range.<br />

349

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

Saved successfully!

Ooh no, something went wrong!