Click to See Complete Forum and Search --> : Menu Automation Script & Concatenating Variables


ronb
01-07-2003, 05:27 PM
Hello,

I'm trying to create a basic menu navigation system. What I want it to do is to read in a list of variables (actually, sets of two variables - the words of the link and the actual href of the link) and then spit out some HTML. However, I don't want the user to have to explicitly state how many sets of variables there are; I just want them to be able to define as many of them as they want in a separate file and never touch the script. I guess this would be counting variables. I'm confused on how to actually get the loop to work to use the variable name instead of a string, though. Here's the basic start of it:

//this part would be in some other file
item_1 = "Menu Item 1";
item_url_1 = "/test1.html"
item_2 = "Menu Item 2";
item_url_2 = "/test1.html"
item_3 = "Menu Item 1";
item_url_3 = "/test1.html"

/*this would be in the main script file, and handle the output of the script. I haven't written the rest of it yet :) */
count = 1;

function writeout(){
while(item_+count)
{
document.write(item_+count);
count++;
}
}

The problem is that it won't concatenate the variable name with the count. Is it possible to do this, or should I find a different approach to counting a number of variables?

Thanks!
RoNb

ronb
01-07-2003, 06:41 PM
Alright, here's an update on what I've been trying to do.

item_1 = "Menu Item 1";
item_url_1 = "/test1.html"
item_2 = "Menu Item 2";
item_url_2 = "/test1.html"
item_3 = "Menu Item 1";
item_url_3 = "/test1.html"
count = 1;

function varconcat(nums)
{
if ("item_" + nums)
{
return "item_" + nums;
}
}

function writeout(){
while(varconcat(count))
{
document.write(varconcat(count));
count++;
}
}


I tested what the return value of the varconcat function is and it seems to give me what I want. However, the javascript treats it as a string, or so it seems, and it therefore never stops looping. How could I make this check against the variable to stop when it has none left?

Thanks again!