Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery bind("load")

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.

like image 479
David Horák Avatar asked Apr 27 '11 15:04

David Horák


1 Answers

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:

  • javascript, image onload() doesnt fire in webkit if loading same image

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
like image 66
Pekka Avatar answered Sep 21 '22 13:09

Pekka