23

If I have a beacon:

<img src="http://example.com/beacon" />

I want a method to be called once the beacon request finishes. Something like:

<script>
    $("img.beacon").load(function() {
        // do stuff knowing the beacon is done
    });
</script>

Is it possible? Is it in jQuery?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Paul Tarjan
  • 48,968
  • 59
  • 172
  • 213

7 Answers7

36

Sure. Remember the load needs to be added before the src attribute.

$('<img />').load( function(){
  console.log('loaded');
}).attr('src', imgUrl);

If you have defined the image tag in the markup then your stuck with when the window load event fires to be sure the image has come down the wire.

redsquare
  • 78,161
  • 20
  • 151
  • 159
19
$('img.beacon').load()

Will not always work correctly, ussually I do this:

$.fn.imageLoad = function(fn){
    this.on('load', fn);
    this.each( function() {
        if ( this.complete && this.naturalWidth !== 0 ) {
            $(this).trigger('load');
        }
    });
}

The above code also covers cases where the image has finished loading before the script is executed. This can happen when you define the image in the markup.

Use like this:

<img src='http://example.com/image.png' class='beacon' />
<script type='text/javascript'>
$(document).ready(function(){ //document.ready not really necessary in this case
   $('img.beacon').imageLoad(function(){
      //do stuff
   });
});
</script>
Onno Eberhard
  • 1,341
  • 1
  • 10
  • 18
Pim Jager
  • 31,965
  • 17
  • 72
  • 98
4
<script type="text/javascript">
    $().ready(function() {
        $('img#beacon').attr('src', 'http://sstatic.net/so/img/so/logo.png')
            .load(function() { alert('Image Loaded'); })
            .error(function() { alert('Error Loading Image');
        });
    });
</script>

This will load the StackOverflow logo.

If it loads successfully, the alert('Image Loaded'); is performed.
If it fails, the alert('Error Loading Image'); is performed.

Of course, either of these can be replaced with your own functions. As a new user, It refused my image tag but the image tag should only contain the id='beacon' attribute. unless you want to add a class. We're setting the src attribute in code here, typically images that you are monitoring for completion have src values that are set programmatically anyway.

Saleh Mahmood
  • 1,823
  • 1
  • 22
  • 30
FerrousOxide
  • 644
  • 1
  • 5
  • 11
3

Another option, if it suits you: The onload event occurs immediately after a page or an image is loaded.

Such as:

<img ... onload="alert('test');" />
lance
  • 16,092
  • 19
  • 77
  • 136
2

Here's a simple javascript code that works on all browsers:

var myImage = new Image();

myImage.onload = function() {
    var image_width = myImage.width;
    var image_height = myImage.height;
    $('#pictureEl').html('<img width="'+ image_width +'" height="'+ image_height +'" src='+ myImage.src + '></img>');           
};

myImage.src = myUrl;

A jQuery snippet must be doing the same thing, under the hood.

a6hi5h3k
  • 671
  • 3
  • 7
  • I don't think it does it this way, but you can check it out yourself... And doing it the way you did it is plain wrong. You don't use event binding that is **the proper** way of event handling. – Robert Koritnik Nov 08 '09 at 13:13
  • This most definately doesn't work in all browsers which is why I am here. Fails in chrome on linux and mac and also on the default android browser, which is actually every browser I have tried so far. – Nathan Schwermann Feb 14 '13 at 06:51
1

You can do it easily in two way.lets see the ways:

1.Using onload attribute in img tag:

<img onload="alert('loaded');" src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png">

This will trigger alert just after the photo is finished loading.

2.Using EventListener in JavaScript:

document.getElementsByTagName("img").addEventListener("load", function loaded(){
  //any code you may need
  }); 

This code goes in your external js file or internal script tag.

You can do this for every individual img tag using class and id attributes.

0

You could use the onload event which fires when the entire page has finished loading.

$(window).load(function(){
  //everything is loaded
});
ichiban
  • 6,162
  • 3
  • 27
  • 34
  • 1
    This is good for `everything` as you mentioned. What if we have 50 items to load and the image is the first of the 50? We do not want to wait till all other 49 items done loaded as well to do something. Also note, this includes stylesheets etc. – Long Huynh Apr 29 '20 at 05:28