6
var imgLoader = $("<img />");
$(imgLoader).attr("src", "http://localhost/malevil/Content/Images/img_adrenalin.jpg");

$(imgLoader).unbind('load');
$(imgLoader).bind('load', function () {
    alert("event fired");
});

But this work only in chrome, where is the problem ? In IE, Firefox and Opera load event never fired.

David Horák
  • 5,535
  • 10
  • 53
  • 77

2 Answers2

12

You need to bind the load event before you set the src property.

As a side note, there are known issues with the load event on images that you need to be aware of:

And, quoting from the jQuery manual on load():

  • It doesn't work consistently nor reliably cross-browser
  • It doesn't fire correctly in WebKit if the image src is set to the same src as before
  • It doesn't correctly bubble up the DOM tree
  • Can cease to fire for images that already live in the browser's cache
Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
3

I would set the handler before you set the src attribute --as it might be that the image loads before your event handler gets set.

Liv
  • 6,006
  • 1
  • 22
  • 29