views:

51

answers:

1

Hi.

I recently did a small jQuery snippet that allows me to show a loading img until the real image is loaded.

The snippet seems to work in Safari, Chrome but not FireFox.

FireFox only displays a loading alt and never switches to the loaded image.

Here is the snippet

var loading = $('<img src="/media/ajax-loader.gif" alt="loading" />');
    $('.thumbnail').each(function(){
        var loadIMG = loading.clone();
            $(this).after(loadIMG).load(function(){
                $(this).fadeIn('fast');
                loadIMG.hide();
        }).hide();
    });

Any ideas why?

+1  A: 

You haven't said what exactly is happing on FF but below can be one of the problem. From jquery documentation

It is possible that the load event will not be triggered if the image is loaded from the browser cache. To account for this possibility, we can use a special load event that fires immediately if the image is ready. event.special.load is currently available as a plugin.

Here's the link for plugin.

Edit:

Based on comments from load event, try below:

$('.thumbnail').each(function(){
        var loadIMG = loading.clone();
            $(this).after(loadIMG).load(function(){
                $(this).fadeIn('fast');
                loadIMG.hide();
        }).hide();
        if (this.complete) $(this).trigger("load");
});

Of course, the plug-in seems to be doing same thing along with handling some other scenarios as well as.

VinayC
Thanks, I just amended the original text, it never switches from the alt to the loaded image.
ApPeL
@ApPeL, above can be the issue - unless load is not triggered on the image, you are not going to see loaded image. Try above plugin and see if it works or not.
VinayC
The above plugin causes all my other jQuery stuff to not work.
ApPeL
@ApPel, plugin seems to be straight-forward. Perhaps, you can try similar code your self - I have edited the answer to list the code.
VinayC