Click here to Skip to main content
16,023,224 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to make a tree structure populated by Html dom tree. I write a recursive function but during page load the page becomes inresponsive.

here is my code

JavaScript
var root=document.documentElement;
                var troot=document.getElementById("list");
                traverse(root, troot);
                function insertAfter(newNode, referenceNode)
                {
                    referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
                }
                function traverse(parent, tparent)
                {
                    var sib=document.createElement("li");
                    sib.appendChild(document.createTextNode(parent.nodeName));
                    tparent.appendChild(sib);
                    var nextSib, nextRoot;
                    nextSib=document.createElement("li");
                    nextRoot=document.createElement("ul");
                    nextSib.appendChild(nextRoot);
                    insertAfter(nextSib, sib);
                    var childs=parent.children;
                    
                    for(var i=0; i<childs.length; i++)
                    {
                        traverse(childs[i], nextRoot);
                    }
                }


What I have tried:

I've tried this using jquery and not working. I'm confused javascript supports recursion or not...
Posted
Updated 10-Aug-16 2:36am

1 solution

You're modifying the DOM tree as you're traversing it. Each time you call traverse, you add a new <li> element to the list, which gives you another element to traverse, which means you call traverse again, which adds another new <li> element, ...

Don't modify the document until you've finished traversing it. The simplest option is probably to create a detatched <ul> element to act as the parent, and append it to a <div> when you've finished:
JavaScript
var root = document.documentElement;
var troot = document.createElement("ul");

traverse(root, troot);

document.getElementById("listContainer").appendChild(troot);
 
Share this answer
 
Comments
Member 11987268 10-Aug-16 9:34am    
Plz give full code. I'm beginner of javascript.
or can I use for loop instead of recursion?
Richard Deeming 10-Aug-16 9:37am    
What do you mean by "full code"? It's calling exactly the same function you posted in your question!
Member 11987268 10-Aug-16 9:57am    
But its hang the browser
Richard Deeming 10-Aug-16 9:59am    
Did you even read my answer?!

Your code is hanging the browser because you're modifying the DOM at the same time that you're traversing it.

Change how you call your function, as I described in my answer, so that you don't change the DOM until you've finished traversing it.
Member 11987268 10-Aug-16 10:04am    
Oh! thnx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900