3

i want to create a new variable with a variable name, for example:

function preloader(imglist) {

  var imgs = imglist.split("|");

    for (i in imgs) {

      var img_{i} = new Image;  //this is the line where i sucked in!
      img_{i}.src = imgs[i];    //another line where i sucked in!

    }
}

preloader("asd.jpg|qwe.jpg|dorama.png");

i try to use arrays but, hmm.. how can i say...

var qwe = new Array();
qwe[1] = "asd"; //or not! whatever...

var qwe[1] = new Image; // it didnt work!

in php u can use like this:

$var1 = "test";
$var2_{$var1} = "test last";
echo $var2_test; //or...
echo $var2_{$var1};
2
  • 1
    Why do you want an unique name on the variable? In your example I see no reason at all to user "dynamic variable names". Just use plain "var img = new Image();" Commented Oct 19, 2010 at 15:18
  • Yes, you don't need unique names - when you say img = new Image(), it doesn't overwrite the old img value. You don't need this PHP weirdness. Commented Oct 19, 2010 at 15:33

3 Answers 3

2

You don't need to do that here, each Image can be stored and overwritten to get the same preload effect, however just this will work:

function preloader(imglist) {
  var imgs = imglist.split("|");
  for (var i=0; i<imgs.length; i++) {
    new Image().src = imgs[i];
  }
}

Note that I changed this from a for in loop, you should use a normal index for loop when iterating over an array (not enumerating) CMS has a great answer here explaining this in more detail.

4
  • 1
    Well, it will work, but the img variable's scope is the function, not the for loop. Commented Oct 19, 2010 at 15:24
  • @Tim - good point, updated though since it's not even needed their either unless you intended to use it. Commented Oct 19, 2010 at 15:27
  • thanks for the answer, but why for loop? i mean when i use like this: var imgs = Array(); imgs[1] = "asd"; imgs[2] = "qwe"; /*and then*/ var i; for(i in imgs) { document.write(imgs[i] + "<br />"); } // is working... Commented Oct 20, 2010 at 7:28
  • @dreamlore - Click the great answer link for a lengthy explanation on this, basically you should never use a for in to iterate an array, as it's enumerating the object (and any other properties it may have) not iterating it. Commented Oct 20, 2010 at 9:26
0

http://www.mojavelinux.com/articles/javascript_hashes.html

0
0

You can achive your goal by using Eval() function as shown below;

var data = "testVariable";
eval("var temp_" + data + "= new Array();");
temp_testVariable[temp_testVariable.length]="Hello World";
alert("Array Length: " + temp_testVariable.length);
alert("Array Data: " + temp_testVariable[0]);

Of course it doesn't mean that is the right solution. I have just given the information about the ability.

Here is the article that contains the above sample and an alternative solution using Window object.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.