-1

How can i use my rand in window name in the following code :

var rand = 185656;
setTimeout("win+rand+2 = window.open('url'+secid , '_blank')",2000);

+rand+ not work

Mohammad Alipour
  • 55
  • 1
  • 2
  • 8

6 Answers6

2

Don't pass a string to setTimeout. Pass a function instead and set your global property explicitly on window, which will allow you to use square bracket notation:

setTimeout(function () {
    window["win" + rand + "2"] = window.open("url" + secid, "_blank");
}, 2000);

The reason for not passing a string to setTimeout (or setInterval) is that it's an alias for eval, which can be dangerous.

What you currently have will generate a reference error. It's effectively like doing this, which is obviously never going to work:

"a" + "b" = "c"; // ReferenceError: Invalid left-hand side in assignment
James Allardice
  • 164,175
  • 21
  • 332
  • 312
  • 1
    Thank you for being the only one, other than me in the comments, to advise against passing a string... – Jeff Shaver Mar 28 '13 at 11:45
  • Thank you , can you please answer my last question? I wonder how to use var in funtion name like funtion one+rand+() inwhich rand is var and one is name – Mohammad Alipour Mar 28 '13 at 12:59
  • @user2100087 - The same way, but set the value of the property to be a function instead: `window["one" + rand] = function () {};`. – James Allardice Mar 28 '13 at 13:02
1

It must be outside the quotes.

setTimeout("win"+rand+"2 = window.open('url'+secid , '_blank')",2000);
Aioros
  • 4,373
  • 1
  • 18
  • 21
1

You need to "open" and "close" the String again:

setTimeout("win" + rand + "2 = window.open('url'" + secid + " , '_blank')",2000);

See this article for the very basics.

Apart from that you shouldn't be passing executable code to setTimeout (it will get eval'd), use a function reference or an anonymous function to do so.

Also, constructiong var names like this is a bad idea as well. Look into objects and keys for that.

You could do:

setTimeout(function(){
    var rand = 185656;
    window['win' + rand + '2'] = window.open('url'+secid , '_blank'); //creates a global variable by adding a property to the global object
}, 2000);
m90
  • 11,434
  • 13
  • 62
  • 112
  • thank you , How can I use rand in this code? win+rand+1 = window.open('url'+secid , '_blank'); is it correct? – Mohammad Alipour Mar 28 '13 at 11:48
  • No, you can't use a runtime variable value for a variable name. You could do something like `window['win'+rand+'1']` as someone already suggested. – Aioros Mar 28 '13 at 11:53
1

Firstly, don't pass strings to setTimeout. Use the callback function instead. Then the variable has to be outside of the string (quotes) to be concatenated:

var rand = 185656;
setTimeout(function() {
    window["win" + rand + "2"] = window.open('url'+secid , '_blank');
}, 2000);
Community
  • 1
  • 1
Richard Dalton
  • 35,513
  • 6
  • 73
  • 91
0

May be the quotes is making problem

setTimeout("win"+parseInt(rand)+"2 = window.open('url'+secid , '_blank')",2000);
웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
0

setTimeout("win" + rand + "2 = window.open('url'+secid , '_blank')",2000);

Dolo
  • 966
  • 14
  • 28