Javascript var in recursive calls

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ger
    New Member
    • Feb 2007
    • 5

    #1

    Javascript var in recursive calls

    My problem is related to this thread. I wrote an XML DOM parser. It parses the complete tree on IE, but it stops after the first downstream on FireFox, SeaMonkey and Opera. This is a simplified code that reproduces the issue:

    Code:
    function PrintNode(node)
    {
        AddText("<b>"+node.nodeName+"</b>="+node.nodeValue+" ("+node.childNodes.length+" children)");
        if (!node.hasChildNodes) return;
        AddText("<ul>");
        var child=node.firstChild;
        do
        {
            if (ShowMsg && !confirm(Text)) return;
            AddText("<li>");
            PrintNode(child);
            AddText("</li>");
        }
        while (child=child.nextSibling);
        AddText("</ul>");
    }
    So, why is this working in IE only? Maybe you can point me to some JScript/JavaScript difference. (A JavaScript/C++ comparison would also help.)

    nb. I've already found a solution that works on all browsers:

    Code:
        for (var child=node.firstChild;child;child=child.nextSibling)
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Welcome to The Scripts.

    Try using the childNodes array as shown in this page (see the source code).

    Comment

    Working...