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,821
  • 10
  • 45
  • 52
James
  • 42,081
  • 53
  • 136
  • 161

4 Answers4

4

Check out this answer

jQuery event for images loaded

Community
  • 1
  • 1
macarthy
  • 3,074
  • 2
  • 23
  • 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,870
  • 3
  • 25
  • 32
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
  • 17,563
  • 3
  • 40
  • 62
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
  • 42,480
  • 25
  • 113
  • 143