Entry
I am getting a Java security error using getChildNodes()in NN6. The code was working fine in NN4.
Nov 1st, 2002 15:59
Mark Filipak, Tejinder Sahi,
Here's your specific answer: Use this:
childNodes.item(i) // where 'i' is a number.
But that doesn't nearly tell the whole story, so here's the whole story.
NN6 is W3C DOM compatible. To get child nodes in W3C DOM2 you have to
use childNode.item(i), where 'i' is the the child number in the parent
node's child list. Perhaps an example is the easiest way to illustrate this.
window.document.childNodes.item
is the method (function) to 'get' the document's top level child nodes, and
window.document.childNodes.length
is the number of direct child nodes of window.document. Thus:
window.document.childNodes.item(0) // [object DocumentType]
window.document.childNodes.item(1) // [object HTMLHtmlElement]
Continuing this 'walk' through the nodal interfaces...
window.document.childNodes.item(1).childNodes.item(0) // [object
HTMLHeadElement]
window.document.childNodes.item(1).childNodes.item(1) // [object
Text], usually just a space char.
window.document.childNodes.item(1).childNodes.item(2) // [object
HTMLBodyElement]
Get it? This 'walk' could be written like this:
window.document.childNodes.item(i)[.childNodes.item(j) ...]
because the nodal interface structure continues, down and out until
leaves are encountered. A leaf can be identified like this
if (currentNode.length==0) alert("I'm a leaf!");
where currentNode is your variable that is the concatination of all of
the '.childNodes.item(i)' that you have traversed to that point,
starting at 'window.document' as shown above.
Of course, to identify a specific element (by tag name or some other
distinguishing attribute), you have to test the other properties of the
child nodes that you find.
There are many shortcuts to get to specific elements without 'walking'
the document tree's nodal structure. The most popular of these shortcuts
is this:
document.getElementById('myId')
where 'myId' is the same name used in the named STYLE
ex: <STYLE>#myId {...CSS rules here...}</style>, *and* in a DIV or SPAN
ex: <DIV id='myId'>...DIV content here...<\DIV>.
Searching the document tree's nodal interfaces is error prone, but if
you must do it, be sure to thoroughly test your code's coverage so that
it doesn't miss any branches. When you 'walk' the document tree's nodal
interfaces you are doing exactly what the rendering engine does!
-- Mark Filipak