Preload Images. Please, help me out. Thank You.

S

shapper

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.Count
img = new Image();
img.src = imagesFolder + imagesNames(i)
End

Is this a good approach?
Could someone tell me the javascript code for this?
I am not very confortable with javascript.

Thank You,
Miguel
 
M

mick white

shapper said:
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.Count
img = new Image();
img.src = imagesFolder + imagesNames(i)
End

imgs=["a.gif","../b.gif","http://example.com/imgs/c.gif" ...],pics=[];
for(var i=0;i<imgs.length;i++){
pic=new Image()
pic.src=imgs;
}

Something like that
Mick
 
W

webEater

shapper said:
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.Count
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.length;i++){
pic=new Image()
pic.src=imgs;

}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.appendChild them to your
site and simply set display="none" or visibility="hidden", 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.length;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.createElement('img'); // probably the same as new
Image(), I dont know.
im.src=imgs;
im.style.width = im.style.height = '0';
im.style.visibility = 'hidden';
// I am not sure if your browser loads them using display="none"
}

Andi
 
A

ASM

shapper a écrit :
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.Count
img = new Image();
img.src = imagesFolder + imagesNames(i)
End

imgs=["a.gif","../b.gif","http://example.com/imgs/c.gif" ...],pics=[];

function postLoad(k, max){
if(k<max) {
pic[k] = new Image();
pic[k].onload = function(){postLoad(k,max)};
pic[k].src = imgs;
k++;
}
}

onload = function(){ postLoad(0, imgs.length); };
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top