views:

144

answers:

5

hi Friends,

I have a js function

I want to assign a variable to a variable

my variable is in a forloop

I have two variables

ie;

var spcd_1= "http://www.colbridge.com";
var spcd_2 = "http://www.google.com";

below is my js function

function openNewWindow(spcd) {
//alert("hello");
var tt = spcd;
alert(tt);
var i=0;
var spcd_1= "http://www.colbridge.com";
var spcd_2 = "http://www.google.com";
for(i=1;i<3;i++)
{
var theurl="'spcd_'+i";
 popupWin = window.open(theurl,
 '_blank',
 'menubar, toolbar, location, directories, status, scrollbars, resizable, dependent, width=640, height=480, left=0, top=0')

}
}

my problem is here

var theurl=spcd_+i;

I want to change theurl value to spcd_1 and spcd_2

how to assign this correctly in the for loop

var theurl=spcd_+i;

can any one show me the correct method.

Thanks

+6  A: 

I think you should just put all the urls in an array, and loop thru it by index.

Javascript does not allow you to use a string to simulate a variable name, so create an array

var urls = ["link1","link2","link3"]; //Add as many urls you need here

and in the for loop, loop it

for (var i=0;i<urls.length;i++) {
//logic here
window.open(urls[i], '_blank',
 'menubar, toolbar, location, directories, status, scrollbars, resizable, dependent, width=640, height=480')
//Do your thing
}
futureelite7
Actually, JavaScript does allow it.
Nosredna
By using eval() ?
futureelite7
No, see my answer below.
Nosredna
and the second half of my answer (the just for giggles bit). Being able to use a string to access a property is mighty handy at times
Dan F
+5  A: 

You should use an array, like this:

var urls = [ "http://www.colbridge.com", "http://www.google.com" ];

for(var i = 0; i < urls.length; i++) {
    window.open(urls[i], '_blank',
     'menubar, toolbar, location, directories, status, scrollbars, resizable, dependent, width=640, height=480')
}

You should probably also read a book about Javascript.

SLaks
A: 

try var = "spcd_"+i

Rod
This will simply assign the variable with the string "spcd_n", where n is the current number. That won't do what the asker wants.
futureelite7
A: 

Sounds like you want to index an array, rather than trying to concatenate yourself up a variable name. Here's your code modified

function openNewWindow(spcd) {
    //alert("hello");
    var tt = spcd;
    alert(tt);
    var i=0;
    var spcds = [];
    spcds.push("http://www.colbridge.com");
    spcds.push("http://www.google.com");
    for(i=0;i<spcds.length;i++)
    {
        var theurl=spcds[i];
        popupWin = window.open(theurl,
        '_blank',
        'menubar, toolbar, location, directories, status, scrollbars, resizable, dependent, width=640, height=480, left=0, top=0')

    }
}

Just for giggles, here's how you could keep using concatenation to get to your variable name. Objects in javascript can also be associative arrays, meaning you can use the array syntax to create and retrieve arbitrary properties

    var spcds = {};
    spcds['spcd_1'] = "http://www.colbridge.com";
    spcds['spcd_2'] = "http://www.google.com";
    //...
    var theurl = spcds['spcd_' + i];
Dan F
A: 

With a global variable, you could do

window["theurl_"+i];

because global variables are properties of window, and you can get at properties with either dot notation or bracket notation.

But don't do that. :-)

A good reason not to even try to do what you're doing is that JavaScript does not have block scoping, so putting vars anywhere but at the beginning of functions is likely to lead to confusion. You get into trouble sooner or later if you put variable declarations at the beginning of loops or conditional blocks.

Nosredna