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
-
This question is similar to: Is there a JavaScript equivalent of PHP’s "variable variables"?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem.dumbass– dumbass2024年10月09日 07:19:03 +00:00Commented Oct 9, 2024 at 7:19
5 Answers 5
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.
Comments
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];
Comments
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.
Comments
Try the following snippet:
var my_var = "spcd_" + i;