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
  • 16,901
  • 19
  • 88
  • 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
  • 93,746
  • 17
  • 112
  • 129
0

You can use the load event as described in jQuery documentation

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