Javascript: dynamic variable names...

  • stevegmag
  • Novice
  • Novice
  • User avatar
  • Posts: 29
  • Loc: Washington DC

Post 3+ Months Ago

I have this javascript code that works for Moziila/NS but not for IE on a PC. I haven't tested on any browser on a Mac yet, so if you know this isn't gonna work there, please let me know.

The goal is to dynamically build my preload images by creating a new image object and assigning the src to it (as is standard practice).

Assume that navOffArr and navOverArr arrays contain the location paths of the images (ie: /images/nav/nav1.gif)


Code: [ Select ]
function buildImages() {
    for (i=0; i < totalNavItems; i++) {    
        eval('rightNav' + i + ' = new Image()');
        eval('rightNav' + i + '.src = navOffArr['+ i +']');
        alert(eval('rightNav' + i + '.src'));
        eval('rightNav' + i + '_over = new Image()');
        eval('rightNav' + i + '_over.src = navOverArr['+ i +']');    
    }
}
  1. function buildImages() {
  2.     for (i=0; i < totalNavItems; i++) {    
  3.         eval('rightNav' + i + ' = new Image()');
  4.         eval('rightNav' + i + '.src = navOffArr['+ i +']');
  5.         alert(eval('rightNav' + i + '.src'));
  6.         eval('rightNav' + i + '_over = new Image()');
  7.         eval('rightNav' + i + '_over.src = navOverArr['+ i +']');    
  8.     }
  9. }


If there's no error in the code, or it works for your IE, or it not possible, please let me know.

If you have working code that does this, please send it along.


thanks is advance
  • stevegmag
  • Novice
  • Novice
  • User avatar
  • Posts: 29
  • Loc: Washington DC

Post 3+ Months Ago

yes I know that I could simply write the code by hand...

Code: [ Select ]

rightNav1 = new Image();
rightNav1.src = navOffArr[i];
rightNav1_over = new Image();
rightNav1_over.src = navOverArr[i]; 
  1. rightNav1 = new Image();
  2. rightNav1.src = navOffArr[i];
  3. rightNav1_over = new Image();
  4. rightNav1_over.src = navOverArr[i]; 


The issue is I don't know how many images there are going to be. It's something that can and will change. The off and over arrays are dynamically population from a nav image directory list.

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 62 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.