Sunday, 15 April 2012

Variable assignment in JavaScript -


I have a JS function

I want to specify a variable in a variable

My variable is in a forloop

I have two variables

i.e.

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

My JS function is

  function OpenNetwork (SPCD) {// Alert ("Hello"); Var tt = spcd; Warning (tt); Var i = 0; Var spcd_1 = "http://www.colbridge.com"; Var spcd_2 = "http://www.google.com"; (I = 1; i  

My problem is here

  var theurl = spcd_ + i; I  theurl  value to  spcd_1  and  spcd_2  

To properly assign a loop to how to change it,

  var theurl = spcd_ + i;  

Anyone can show me the right way.

Thank you

Looks like you add yourself a variable name Instead of trying, you want to index an array. Your code has been modified

  function OpenNewWondo (SPCD) {// Alert ("Hello"); Var tt = spcd; Warning (tt); Var i = 0; Var spcds = []; Spcds.push ("http://www.colbridge.com"); Spcds.push ("http://www.google.com"); (I = 0; i & lt; spcds.length; i ++) for {var theurl = spcds [i]; PopupWin = window.open (theurl, '_blank', 'menubar, toolbar, location, directory, status, scrollbar, resizable, dependent, width = 640, height = 480, left = 0, top = 0')}}  

Just for giggles, here's how you can use containment to get your variable name. Javascript can also contain objects, which means that you can use the ARA syntax to create and retrieve arbitrary properties

  var spcds = {}; SPCDDS ['SPCDD' '] = "http://www.colbridge.com"; Spcds ['spcd_2'] = "http://www.google.com"; // ... var theurl = spcds ['spcd_' + i];  

No comments:

Post a Comment