2

Possible Duplicate:
javascript executing function after full image load

Im using JQuery to programmatically change the html img:

$('#myimage').attr('src', 'url-of-image');

That may take some time depending on the url-of-image.

How can we add an event that will trigger if the new image was loaded successfully?

Community
  • 1
  • 1
JR Galia
  • 17,229
  • 19
  • 92
  • 144

3 Answers3

4
$('#myimage').attr('src', 'url-of-image').load(function() {
   // after load
});

Edit: working example http://jsfiddle.net/yaSWM/

Edit 2: this works fine on all major browsers.

lukedays
  • 323
  • 1
  • 10
3
$('#myimage').attr('src', 'url-of-image').on('load', function(){
    //image loaded
});
Musa
  • 96,336
  • 17
  • 118
  • 137
0

You can use the load event as described in jQuery documentation

$('#myimage').load(function() {
  //do whatever
}
jaudette
  • 2,305
  • 1
  • 20
  • 20