By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,466 Members | 1,148 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,466 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a


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

P: n/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[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.