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

preloading images not working in firefox

P: n/a
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
function preloadImages(images) {
var imgPreload=new Array();
var total=images.length;
var now=0;

for(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];
};
}

Oct 2 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.
Are you seeing the difference in the way IE and FF cache images? What
happens if you clear the cache in FF?
--
Ian Collins.
Oct 2 '06 #2

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

Oct 2 '06 #3

P: n/a
Ok, thanks for the answers.

First of all let me tell you that the page I'm preloading the images
for is in a new window (kind of popup) and there my preloader stops
before loading everything, instad if I call the page directly in
Firefox (without popping out the window) this script works fine.

Then, thanks to your advices, I've changed the variable name from
"images" to "myImages" and I've set the "i" variable as local.

And still didn't work fine.

Finally I've changed the imgPreload array from local to global and
suddeny evertyhing worked out. So yep I suppose it was something
relates to garbage collector as you told.

This is not a rollover-images preloader, I want to preload many images
(kind of big ones) in an horizontal-scrolling layout, so I want to
prevent my users from scrolling to not fully loaded sections.

Thank you.
Bye
carlo

Oct 5 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.