On 25 Nov., 18:31, mick white <m...@mickweb.c omwrote:
shapper wrote:
Hello,
I think to preload an image I should us something like:
img = new Image();
img.src = 'images/img.jpg';
Could someone tell me how to create a loop which would preload a list
of images?
Something like:
ImagesFolder = '...';
ImagesNames = '...';
For i = 0 to ImagesNames.Cou nt
img = new Image();
img.src = imagesFolder + imagesNames(i)
Endimgs=["a.gif","../b.gif","http://example.com/imgs/c.gif" ...],pics=[];
for(var i=0;i<imgs.leng th;i++){
pic[i]=new Image()
pic[i].src=imgs[i];
}Something like that
Mick
Is this a good approach?
Could someone tell me the javascript code for this?
I am not very confortable with javascript.
Thank You,
Miguel
Better create REAL images, means document.body.a ppendChild them to your
site and simply set display="none" or visibility="hid den", so they are
really preloaded. My experience with new Image() ... is bad, not all
browser preload them onload. Example
imgs=["a.gif","../b.gif","http://example.com/imgs/c.gif" ...],pics=[];
for(var i=0;i<imgs.leng th;i++){
// why make an image array, excepting you want get the images
explicitly. Important is, that the browser loads the image data.
var im = document.create Element('img'); // probably the same as new
Image(), I dont know.
im.src=imgs[i];
im.style.width = im.style.height = '0';
im.style.visibi lity = 'hidden';
// I am not sure if your browser loads them using display="none"
}
Andi