469,909 Members | 1,630 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,909 developers. It's quick & easy.

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

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

Nov 25 '06 #1
4 1545
shapper escreveu:
Is this a good approach?
Could someone tell me the javascript code for this?
I am not very confortable with javascript.
Take a look if this helps: <URL:http://jsfromhell.com/classes/preloader>
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Nov 25 '06 #2
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.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[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
Nov 25 '06 #3


On 25 Nov., 18:31, mick white <m...@mickweb.comwrote:
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.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[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.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[i];
im.style.width = im.style.height = '0';
im.style.visibility = 'hidden';
// I am not sure if your browser loads them using display="none"
}

Andi

Nov 25 '06 #4
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[i];
k++;
}
}

onload = function(){ postLoad(0, imgs.length); };
--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Contact : http://stephane.moriaux.perso.wanadoo.fr/contact
ASM = Aimable Stéphane Moriaux = Amateur Sasseur Merdouilles
Nov 26 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by MALdito | last post: by
3 posts views Thread by Bob | last post: by
2 posts views Thread by Albert Spencil | last post: by
1 post views Thread by Stacey | last post: by
5 posts views Thread by roN | last post: by
9 posts views Thread by shapper | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.