Guardian23
08-06-2002, 10:30 AM
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. :(
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. :(