PDA

View Full Version : Preload images that don't exits



ianric
06-01-2009, 02:34 PM
Hi

I'm using this code to preload images in my e-commerce website. Can't remember where I got the script but it works.

<SCRIPT LANGUAGE="JavaScript">

var aryImages = new Array();

aryImages[0] = "prod_1_A.jpg";
aryImages[1] = "prod_1_B.jpg";
aryImages[2] = "prod_1_C.jpg";
aryImages[3] = "prod_1_D.jpg";

for (i=0; i < aryImages.length; i++) {
var preload = new Image();
preload.src = aryImages[i];
}

</script>

I have around 600 products on my site, some have 2 images, some 4 images etc and would like to have one script that does all instead of 600 slightly different scripts.

So, the question is, what happens if I add two more images, prod_1_E & prod_1_F, to the array even tho they don't exist?

I've tried it and it seems to work. Just wanted to know if it's OK or will I hit probs further down the line.

Cheers

Ian

clueful
06-01-2009, 09:07 PM
No matter how it may seem, that code does not work because it doesn't use a non-volatile array. It will only pre-load the last specified image.

You don't actually need JS to pre-load, but if you're using it anyway you can try this pre-loader:

http://scripterlative.com?preloader

ianric
06-03-2009, 09:04 AM
Hi

Thanks for the reply. I'm using the scripterlative Thumbsmart script and the reason for the pre loading is that the main large image sometimes doesn't display until the 4 thumbs are rolled over then it's OK.

Thinking about it, it's only that image so I can just preload that one and I should be able to do that dynamically with a variable.

Cheers

Ian

clueful
06-03-2009, 09:53 AM
Hi

Thanks for the reply. I'm using the scripterlative Thumbsmart script and the reason for the pre loading is that the main large image sometimes doesn't display until the 4 thumbs are rolled over then it's OK.
That shouldn't happen - do have a URL?