Helpful Information
 
 
Category: DOM and JSON scripting
DOM traverse and alert

Could someone please help me find out where exactly I went wrong with this?
code follows:



<html>
<head><title>
whatever
</title></head>
<body>
<h1></h1><h2></h2><h3></h3>
</body>
</html>
<script>
function spyder(path, path_name, flag){
alert ("Path: " + path_name)
if (path.tagName) {
alert ("<"+path.tagName+">")
} else {
alert ("Start of Document")
}
for (i = 0; i < path.childNodes.length; i++){
path_name = path_name + ".childNodes[" + i + "]"
path = path.childNodes[i] //interchange line#1
spyder (path, path_name, 1) //interchange line#2
/*
//alternatively, change the above two lines to:
spyder (path.childNodes[i], path_name, 1)
*/
//alert (i) I just put this here in case I wanted to use it, but it doesn't seem to help anyway...
if (flag == 1){
alert ("End of Path: " + path_name)
alert ("</"+path.tagName+">")
}
}
if (flag == 0){
alert ("End of Path: " + path_name)
/*originally these two lines weren't necessary ??*/
alert ("</"+path.tagName+">")
/*same as above*/
alert ("End of the Document!")
alert ("Document has been traversed!")
}
}
//spyder (document.body, "document.body", 0)
/*when interchanged, running this function generates a loop
around H2; on default, the closing "</body>" alert doesn't get
displayed, whereas the "End of document!" display does*/
spyder (document, "document", 0)
/*when interchanged, well, it's not quite the same*/
</script>


Now supposedely, when running the default function on the
document, it should alert all of the nested tags in nesting order
--at least, that's what I want it to do.
But in reality, the loop only loops through the bottom layer (i.e.
document.childNodes[0].childNodes[0].............), and when
manually implementing the interchange (declared as such within
the code), it loops around the H2 tag in document.body; but
when there actually is content within the HX tags, it declared
"undefined" a number of times.
Unfortunately, after changing the source code for the second
time, I was unable to duplicate the error and therefore cannot
demonstrate.

In case the information is helpful, the scripts basic function is to
go through the entire document and alert all that it sees in each
path/subpath, the purpose being to enable me to better un-
derstand the actual "personality" of the DOM. After which I will
extend the function to enable an action, and therefore it is vital
that each subpath is only displayed once(I say this because
moving the "if (flag == 1)" argument OUTSIDE of the for loop
seems to cause "</TITLE>" and it's corresponding path to alert
twice).

Any help would be greatly appreciated,
Guardian23

PS. I think this might belong in with the general posts because
the problem revolves around the "for" loop, but it also involves
the DOM, so...... sorry if I posted in the wrong sub-section. :(

Before you continue with this code, please consider the amount of time you will spend closing all those alerts ;)

Alternative way to acomplish the task at hand is to open another window and write the information about the document structure in it. For more info see my implementation of the DOM Node Tree Viewer (http://www.vladdy.net/webdesign/DOM_TreeViewer.html) .

Strictly off the record:

That's probably where Parkinson's comes from.
*forefinger loses control over spacebar*

Guardian

PS. But like I said, I plan on extending it by replacing the "alert"
functions with an action... The main problem seems to generate
within the loop, could it be the fact that all of the "for" loops (in
fact there is only one with a supposedely infinite (well, as many
as there are subpaths) amount of instances) use the same "i".
PSS. also note the fact that the HTML document in question is
extremely short and consists only of first- and second- level
tags (can I do that with a hyphen?)

Guardian *felt the need to sign off again after the lengthy PS*

Use

var i

before the for statement

Well, I'm not sure anymore. I'm gonna post the HTML and library
here, hoping that it'll be easier to understand, I also made a few
changes.

Guardian

You define your "for" loop in a recursive function as:


for (i = 0; i < path.childNodes.length; i++)
{
// Do stuff here
}


In order to initalize variable for every instance of the recursive function you need to declare it within the function:


var i;
for (i = 0; i < path.childNodes.length; i++)
{
// Do stuff here
}

or


for (var i = 0; i < path.childNodes.length; i++)
{
// Do stuff here
}










privacy (GDPR)