236 CHAPTER 8 (Crystaltech web hosting) SCRIPTING IN THE BROWSER
236 CHAPTER 8 SCRIPTING IN THE BROWSER removeChild(oldChild) The removeChild() method removes the old child parameter from the current node s childNodes collection. It returns a reference to the removed node: oCurrentNode = oDocument.documentElement; oOldNode = oCurrentNode.removeNode(oCurrentNode.lastChild); This code removes the last child of the current node. replaceChild(newChild, oldChild) This method replaces a child of the current node with a new child. It returns the replaced child. The following code creates a new node and uses replaceChild to replace the last child element: oNewNode = oDocument.createElement(”title”); oRootNode = oDocument.documentElement; oOldNode = oRootNode.replaceChild(oRootNode.lastChild,oNewNode); Understanding the NodeList Interface The NodeList interface deals with an ordered collection of nodes. Each node in the collection is indexed, starting with 0. You saw earlier that the childNodes property returns a NodeList. You need to be familiar with the length property and item() method. length The length read-only property indicates the length of the NodeList. item (index) The item() method takes an index argument and returns the node at that index from the NodeList: for (var i=0; i < oDocument.documentElement.childNodes.length; i++) { alert(oDocument.documentElement.childNodes.item(i).nodeName); } This code block uses a for loop to iterate through the childNodes collection of documentElement. It pops up an alert box showing the nodeName of each node in that collection. You can also use shorthand syntax to access the list of nodes: alert(oDocument.documentElement.childNodes[i].nodeName); Understanding the NamedNodeMap Interface The NamedNodeMap interface reflects a collection of nodes that you can access by name or index. The collection is not held in any particular order, and you can use the interface to add and delete nodes from within the collection.
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.