li*******@gmail.com wrote:
Hi everybody,
I have a typical javascript images preloader, it works fine both on
Firefox and on IE in my intranet (local server).
It works fine on the Internet (remote server) in IE.
In Firefox it stops before loading every image.
Does image.onload function have some problems with little images?
Any ideas? I suppose there's another post with the same topic, if so
could you please give me the link?
Thank you in advance,
carlo
This is the function:
// images is an array of images name
I'm not sure that's a good name for a variable as there is a
document.images collection already - it might get confusing for someone
maintaining your code.
function preloadImages(images) {
var imgPreload=new Array();
Usually a global variable is used so that it persists after the
function has finished (see below).
var total=images.length;
var now=0;
for(i=0;i<images.length;i++) {
Variables like "i" should be kept local using the var keyword unless
there is a good reason to make them global:
for(var i=0; i<images.length; i++) {
>
imgPreload[i]=new Image();
imgPreload[i].onload=function() {
now++;
if (now==total)
alert("OK!");
}
imgPreload[i].src = images[i];
};
}
Typically your function will finish running well before the images have
finished loading. At that point, any local variables (including the
images array) will be available for garbage collection. It is very
likely that when you use this over the internet, the images haven't all
finished loading when the function ends so some never do.
When used locally, the images load faster and get to complete - the
simple solution is to make your images array global.
If this is just for small menu images, consider using CSS rollovers,
then you don't need any pre-loading (or even script support).
--
Rob