Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loops in Javascript

Status
Not open for further replies.

skytrev

Programmer
Sep 2, 1999
2
US
I'm attempting to create a tree view in JavaScript using values from a database(oledb connection). I put all the values into hidden inputs in the html so I can access them in JavaScript. Here's my challenge:<br>
I set up an loop to display all the parent nodes (Groups of reports), I now need to put a loop inside the other loop to put the child nodes in there (reports). It keeps crashing my browser. Can I nest a 'do...while' inside another in JavaScript? I'm mainly an ASP programmer, don't know nearly enough of JavaScript to get this to work. Any advice would be appreciated.
 
To answer your question directly, yes you can nest do...whiles. I put this example together and tested it with IE5:<br>
&lt;script language="JavaScript"&gt;<br>
var i = 0, j = 0;<br>
do {<br>
i+=1<br>
do {<br>
j+=1<br>
document.write("j="+j+"&nbsp;");}<br>
while (j&lt;5)<br>
document.write("i="+i+"&nbsp;");}<br>
while (i&lt;5);<br>
&lt;/script&gt;<br>
<br>
although as you can see, it makes the code extremely difficult to read and even harder to debug. <br>
<br>
May offer a couple of suggestions? I assume you are using ASP to connect to the DB. If so, use the adOpenKeyset cursor to open your recordset, this will enable the recordcount property. Use the record count property to set the upper limit of a for loop. Cleaner code and more control.<br>
<br>
Even better... abandon the idea of storing all this stuff in form elements and creating a the DB based menu with JavaScript. This sort of activity is much better suited for the server... create the menu with ASP, spit straight HTML to the client and then manipulate it (opens and closes, hilites and images, etc) with DHTML/CSS/JavaScript.<br>
<br>
Or, (and this one's even easier), if you can dictate your user's browser (IE), use the TreeView ActiveX control, generate the nodes with ASP code and let the ActiveX control do all the menu manipulations.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top