Variable assignment in JavaScript
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 openNewWindo开发者_开发技巧w(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
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
}
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.
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];
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.
Try the following snippet:
var my_var = "spcd_" + i;
精彩评论