0
<img src="someimage.jpg" alt="thumbnail" />

I'm changing the src attribute of this image dynamically.

How do I check whether the image was loaded or not? And do something when it is loaded.

Thanks.

ЯegDwight
  • 24,315
  • 10
  • 44
  • 52
James
  • 38,737
  • 52
  • 130
  • 161

4 Answers4

4

Check out this answer

jQuery event for images loaded

Community
  • 1
  • 1
macarthy
  • 3,056
  • 2
  • 22
  • 24
2

Start here: http://forum.jquery.com/topic/simple-image-load-detection-with-load

The above assumes you are loading the image from JQuery and that is where you need to attach your additional code.

Gregory A Beamer
  • 16,680
  • 3
  • 24
  • 30
1

you can handle the load event with...

 $('#id').load(function(){
      //handler code here
 });

If you only want to handle when it is changed...

   $('#id').attr('src','yourimage.png').load(function(){
      //handler code here
 });
Brandon Frohbieter
  • 16,827
  • 3
  • 36
  • 61
0

Check working example at http://jsfiddle.net/6knva/

var link = 'http://i.treehugger.com/images/2007/5/24/120_mph_electric_car.jpg';
$('img').attr('src',link).load(function(){
      alert('image loaded');
 });
Hussein
  • 41,814
  • 25
  • 111
  • 143