views:

70

answers:

5

I want to do:

  $("img").bind('load', function() {
    // do stuff
  });

But the load event doesn't fire when the image is loaded from cache. The jQuery docs suggest a plugin to fix this, but it doesn't work

A: 

Can I suggest that you reload it into a non-DOM image object? If it's cached, this will take no time at all, and the onload will still fire. If it isn't cached, it will fire the onload when the image is loaded, which should be the same time as the DOM version of the image finishes loading.

Javascript:

$(document).ready(function() {
    var tmpImg = new Image() ;
    tmpImg.src = $('#img').attr('src') ;
    tmpImg.onload = function() {
        // Run onload code.
    } ;
}) ;

Updated (to handle multiple images and with correctly ordered onload attachment):

$(document).ready(function() {
    var imageLoaded = function() {
        // Run onload code.
    }
    $('#img').each(function() {
        var tmpImg = new Image() ;
        tmpImg.onload = imageLoaded ;
        tmpImg.src = $(this).attr('src') ;
    }) ;
}) ;
Gus
You should try to attach the `load` handler *before* it's added, also this won't work but for one image, the OPs code works for many :)
Nick Craver
Thanks, I have added an updated version which I hope addresses these two issues.
Gus
@Gus - `#img` is an ID not an element selector :) Also `this.src` works, no need to use jQuery where it isn't needed :) But creating another image seems like overkill in either case IMO.
Nick Craver
A: 

Do you really have to do it with jQuery? You can attach the onload event directly to your image as well;

<img src="/path/to/image.jpg" onload="doStuff(this);" />

It will fire every time the image has loaded, from cache or not.

Björn
+1  A: 

If the src is already set then the event is firing in the cache cased before you get the event handler bound. To fix this, you can loop through checking and triggering the event based off .complete, like this:

$("img").one('load', function() {
  // do stuff
}).each(function() {
  if(this.complete) $(this).load();
});

Note the change from .bind() to .one() so the event handler doesn't run twice.

Nick Craver
A: 

You may want to check out this plugin:

http://github.com/peol/jquery.imgloaded/raw/master/ahpi.imgload.js

Jud Stephenson
This is the exact plugin mentioned in the question :)
Nick Craver
hmm, thought I had a different one. In which case, I would just go with what Nick said.
Jud Stephenson
A: 

A modification to GUS's example:

$(document).ready(function() {
    var tmpImg = new Image() ;
    tmpImg.onload = function() {
        // Run onload code.
    } ;

tmpImg.src = $('#img').attr('src');
})

Set the source before and after the onload.

Chuck Conway
Like @Gus's answer, this won't work for multiple images...and there's no need to set the `src` before the attaching the `onload` handler, once afterwards will suffice.
Nick Craver
@Nick The question doesn't say anything about multiple images...
Chuck Conway
@Chuck - Sure it does, `$("img")` selects *all* `<img>` elements...
Nick Craver
@Nick Doh! I missed that.
Chuck Conway