2

On the initial page load, the image load callback gets invoked. I use knockout to add dynamic images to the page. When I do that, however, the image load callback is not being invoked. Is there a work around for that?

$(function(){
    $("img").on("load", function () { 
       console.log("loaded");
    });
});
user3587180
  • 1,317
  • 11
  • 23

2 Answers2

1

It's likely the onload event has occurred before you've attached the listener, so you could try...

$("img").on("load", function() { 
  // your onload code
}).each(function() {
    // in case it's already loaded
    if(this.complete) $(this).load();
});
user3094755
  • 1,561
  • 16
  • 20
-1
var img = new Image();
img.onload = function () {
   alert("image is loaded");
}
img.src = "img.jpg";

give it a try

Jehad Ahmad Jaghoub
  • 1,225
  • 14
  • 22