Web Analytics Made Easy -
StatCounter Image preloading problems - CodingForum


No announcement yet.

Image preloading problems

  • Filter
  • Time
  • Show
Clear All
new posts

  • Image preloading problems

    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

        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];

  • #2
    You'll only see the last one in cache because you're only using one image object.
    Create a new image object for each image.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit