-1

Possible Duplicate:
JavaScript: Know when an image is fully loaded

I have this code:

$("#imageOnPage").attr("src", urlString);

I need to somehow fire an event after the image becomes visible on the screen. In other words I need an onLoad event for the image.

Community
  • 1
  • 1
Registered User
  • 3,669
  • 11
  • 41
  • 65
  • 2
    possible duplicate of [JavaScript: Know when an image is fully loaded](http://stackoverflow.com/questions/1257385/javascript-know-when-an-image-is-fully-loaded) and [Check if an image is loaded (no errors) in JavaScript](http://stackoverflow.com/questions/1977871/check-if-an-image-is-loaded-no-errors-in-javascript) --- please use the search before you ask a new question, [this has been asked before many times](http://stackoverflow.com/search?q=jquery+test+if+image+loaded). – Felix Kling May 13 '12 at 18:17
  • Some more: http://stackoverflow.com/questions/263359/how-can-i-determine-if-an-image-has-loaded-using-javascript-jquery, http://stackoverflow.com/questions/7712002/how-to-know-whether-or-not-a-image-has-been-loaded-using-javascript – Felix Kling May 13 '12 at 18:20

3 Answers3

4

There is actually an event for that:

$('#imageOnPage').load(function()
{
/* your code */
});

It gets fired when your image has loaded.

Johannes Lumpe
  • 1,762
  • 13
  • 16
1

You should be able to bind to the load event of the image.

$('#imageOnPage').load(function() {
    alert('image loaded');
});
Nathan Taylor
  • 24,423
  • 19
  • 99
  • 156
1
$('#imageOnPage').load(function(){
  $(this).attr('src', urlString);
});
The System Restart
  • 2,873
  • 19
  • 28