Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Image preloading problems

Status
Not open for further replies.

hessodreamy

Programmer
Feb 28, 2005
59
GB
I am trying to preload some images for a rotating adbanner type affair. But my script, which is pretty identical to others on the internet, seems like it only preloads the last picture in the array, having looked at the broswer cache.

Any ideas? Yes I could use something that someone else has written, but that would take away the fun, wouldn't it?

Here's the code

Code:
    if (document.images)
    {
      preload_image_object = new Image();
      // set image url
      path = "../images/rollovers/";
      image_url = new Array();
      image_url[0] = path+"banner-pricepromise1.png";
      image_url[1] = path+"banner-pricepromise2.png";
      image_url[2] = path+"banner-30-day.png";
      image_url[3] = path+"banner-creditcard.png";
      image_url[4] = path+"banner-delivery.png";
      image_url[5] = path+"banner-netbanx.png";
      image_url[6] = path+"banner-online-disc.png";
      image_url[7] = path+"banner-thawte.png";
      image_url[8] = path+"banner-warranties.png";
      image_url[9] = path+"banner-shop.png";


       for(var i in image_url)
         preload_image_object.src = image_url[i];
    }
 
each Image() object represents one single image. you need to create one for each of your images. something like:


var ImageStore = new Array();

ImageStore[0] = new Image();
ImageStore[0].src = "/path/to/img1.png";
ImageStore[1] = new Image();
ImageStore[1].src = "/path/to/img2.png";
...etc..


-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
I am not sure about the clever workaround for that, the one I immediately see is loading all of them into separate objects. The problem with your code is most likely that the image object src is set to some image and then is immediatly set to another, so the first image doesn't actually finish loading (or even start :)) loading.
 
AH yes of course. It seems so obvious now.
Thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top