Maybe is a trivial problem, i don't know why this function exit from for cycle when it goes on else statement. I need this function to fetch an xml document.
function xmlToArray(element){
childs= element.childNodes;
if(childs.length != 1){
for(var i=0;i<childs.length;i++){
if(childs[i].hasChildNodes()){
xmlToArray(childs[i]);
}
alert("exit from if");
}//end for
alert("exit from for");
}//end if
else{
alert("do something with element");
}
alert("end of func");
}
Joel Coehoorn
419k114 gold badges582 silver badges820 bronze badges
asked Feb 26, 2010 at 17:28
Manuel Bitto
5,2836 gold badges43 silver badges48 bronze badges
-
Do you mean it's dropping completely from the stack, every iteration of it, when it encounters the else, or just the level it's on?Tarka– Tarka2010年02月26日 17:30:27 +00:00Commented Feb 26, 2010 at 17:30
1 Answer 1
Since childs is not a local variable, all calls of xmlToArray work on the same data.
Try this:
function xmlToArray(element) {
var childs = element.childNodes;
// ...
}
Using var declares that variable in the current scope.
answered Feb 26, 2010 at 17:33
Gumbo
657k112 gold badges792 silver badges852 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js