tags:

views:

106

answers:

3

I'm doing a very basic image load function in Jquery, this is the code I have:

$(document).ready(function(){  
    var img = new Image();  
    //  
    $(img).load(function () {  
        alert('loaded');  
    }).error(function () {  
        alert('error');  
    }).attr('src', 'images/loader.gif');  
});

In firefox it works fine but in IE7 it just keeps alerting 'loaded' over and over again, if I change the URL so its a broken link it outputs 'error' just once.

Anyone know why this is happening?

A: 

My guess: Assigning a src value triggers a load event in IE7, hence the 'infinite' loop.
If image file does not exist, the load does not complete hence stops there and then.

o.k.w
how does this explain the infinite loop?
redsquare
what o.k.w means probably is : load() -> attr() -> load() -> attr() ... this is infinite, assigning src triggers load which assigns again ...
Soufiane Hassou
I always figured that this was the usual way of doing image loads - I'm using the code from this page btw: http://jqueryfordesigners.com/image-loading/
Benny b
'$(img).load(function ()' = OnImageLoad. `.attr('src', 'images/loader.gif')' assigns a source to the `<img>` resulting in another ImageLoad event, which then assign the source again and.... well endless recursive...
o.k.w
Check out http://msdn.microsoft.com/en-us/library/cc197055%28VS.85%29.aspx. Setting the src causes IE to load the image again, triggering the load event.
BStruthers
@Soufiane: Yea, that's what I'm trying to say, thanks!
o.k.w
Ok I've looked at BStruthers link but not sure how I would work that code into my setup, I just want to preload an image then run a block of code once its loaded. This has a slightly different way of doing it but again the code runs on an infinate loop in IE7: http://jquery-howto.blogspot.com/2009/02/preload-images-with-jquery.html - can anyone help me out?
Benny b
A: 

Ok I've looked at BStruthers link but not sure how I would work that code into my setup, I just want to preload an image then run a block of code once its loaded. This has a slightly different way of doing it but again the code runs on an infinate loop in IE7: jquery-howto.blogspot.com/2009/02/… - can anyone help me out?

Benny b
A: 

Couldn't find a solution to the problem so ended up using the jquery.preload plugin:

http://plugins.jquery.com/project/Preload

Which seems to work fine in IE7

Benny b