Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jul 2002
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts

    DOM traverse and alert

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

    Code:
    <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.
    Last edited by Guardian23; 08-06-2002 at 09:50 AM.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Nashua, NH
    Posts
    1,724
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 .
    Last edited by Vladdy; 08-06-2002 at 12:59 PM.
    Vladdy | KL
    "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

  • #3
    New Coder
    Join Date
    Jul 2002
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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*
    Last edited by Guardian23; 08-06-2002 at 01:10 PM.

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    Nashua, NH
    Posts
    1,724
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Use

    var i

    before the for statement
    Vladdy | KL
    "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

  • #5
    New Coder
    Join Date
    Jul 2002
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Attached Files Attached Files

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    Nashua, NH
    Posts
    1,724
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You define your "for" loop in a recursive function as:
    Code:
    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:
    Code:
    var i;
    for (i = 0; i < path.childNodes.length; i++)
    {
    // Do stuff here
    }
    or
    Code:
    for (var i = 0; i < path.childNodes.length; i++)
    {
    // Do stuff here
    }
    Vladdy | KL
    "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •