Open all child nodes of the root

I need to open all the child nodes of the root.
I use this code :

var a= tree.hasChildren(0);
var i=0;
while(i <a){
var childNode = tree.getChildItemIdByIndex(0,i);
tree.openItem(childNode);
//alert(i);
i++;
}

Now this code runs fine when I uncomment alert reason being alert gives sufficient time for processing and all the child nodes are loaded
Without alert only the last child node is opened.
Can anyone please answer my query as soon as possible, I need the solution urgently.

There is a special method openAllItems(). You can call it for the root item: tree.openAllItems(0)
If you have dynamic tree, use method openAllItemsDynamic() (but pay attention thet it is in PRO version).
Plus if your tree based on xml structure, you need to call described methods in callback on loadXML method:

tree.loadXML("tree.xml", function(){ tree.openAllItems(0) });

I cannot use OpenAllItems() because it will open the children of child nodes as well and I don’t want that.
and Yes the version does not support OpenItemDynamic() method, I tried to use it.
Thank you for the replies though:)
What I wanted was if I could buy some time for the processing to occur like alert does using some timeout or sleep method.

First of all try do not use alert. If you need to check something, use console.log. It will nor interrupt tree opening.

console.log is not working, I tried to use onreadyStateChange function of Ajax to solve the problem, took a clue from the Internet but it is not working.
Can anyone please help me with this code ?

function httpRequest() {
var xmlhttp;

    if (window.XMLHttpRequest) {
    	// code for IE7+, Firefox, Chrome, Opera, Safari
    	xmlhttp=new XMLHttpRequest();
    } else if (window.ActiveXObject) {
    	// code for IE6, IE5
    	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    } else {
    	alert("Your browser does not support XMLHTTP!");
    }

    return xmlhttp;
}
	
function openFirst()
 {
     var xmlhttp = httpRequest();
	 if (tree.getUserData(0,'opened') == 'false')
		  {
			  tree.openItem(0);
			  xmlhttp.onreadystatechange = function() {
		    		if(xmlhttp.readyState == 4)
		    		{	
		    			var response = xmlhttp.responseText;
		    			if(response != "") {

		    				 if(<%=global.isDisplay()%>)
							  {
							  var a= tree.hasChildren(0);
							  var i=0;
							  while(i <a){
								  
								var childNode = tree.getChildItemIdByIndex(0,i);
								tree.openItem(childNode);
								//window.setTimeout(function(){},4000);
							//alert(childNode);
								i++;
						  } } 
		    			}
		    		}
		    	}
						  
		  tree.setUserData(0,'opened', 'true'); 
		  }
	  else
		   {
		   setTimeout('openFirst();',500);
		   }

	 openAll();
}

If you need to open items just on one (say 1th) level, you can try to use the next approach:

tree.loadXML("tree3.xml", function(){ tree.closeAllItems(0); var ch = tree.hasChildren(0); for(var i=0; i<ch; i++){ var lev1 = tree.getChildItemIdByIndex(0,i); tree.openItem(lev1); }