04.11.2015 Views

javascript

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 10: The Document Object Model<br />

This works in all browsers except IE, which throws an error because a NodeList is implemented as a<br />

COM object and thus cannot be used where a JScript object is necessary. To convert a NodeList to an<br />

array in IE, you must manually iterate over the members. The following function works in all browsers:<br />

function convertToArray(nodes){<br />

var array = null;<br />

try {<br />

array = Array.prototype.slice.call(nodes, 0); //non-IE<br />

} catch (ex) {<br />

array = new Array();<br />

for (var i=0, len=nodes.length; i < len; i++){<br />

array.push(nodes[i]);<br />

}<br />

}<br />

}<br />

return array;<br />

The convertToArray() function first attempts to use the easiest manner of creating an array. If that<br />

throws an error (which it will in IE), the error is caught by the try - catch block and the array is created<br />

manually. This is another form of quirks detection.<br />

Each node has a parentNode property pointing to its parent in the document tree. All nodes contained<br />

within a childNodes list have the same parent, so each of their parentNode properties points to the<br />

same node. Additionally, each node within a childNodes list is considered to be a sibling of the other<br />

nodes in the same list. It ’ s possible to navigate from one node in the list to another by using the<br />

previousSibling and nextSibling properties. The first node in the list has null for the value<br />

of its previousSibling property, and the last node in the list has null for the value of its nextSibling<br />

property, as shown in the following example:<br />

if (someNode.nextSibling === null){<br />

alert(“Last node in the parent’s childNodes list.”);<br />

} else if (someNode.previousSibling === null){<br />

alert(“First node in the parent’s childNodes list.”);<br />

}<br />

Note that if there ’ s only one child node, both nextSibling and previousSibling will be null .<br />

Another relationship exists between a parent node and its first and last child nodes. The firstChild<br />

and lastChild properties point to the first and last node in the childNodes list, respectively. The value<br />

of someNode.firstChild is always equal to someNode.childNodes[0], and the value of someNode<br />

.lastChild is always equal to someNode.childNodes[someNode.childNodes.length - 1] . If there<br />

is only one child node, firstChild and lastChild point to the same node; if there are no children,<br />

then firstChild and lastChild are both null . All of these relationships help to navigate easily<br />

between nodes in a document structure. Figure 10 - 2 illustrates these relationships.<br />

265

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

Saved successfully!

Ooh no, something went wrong!