Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript: Know when an image is fully loaded

Tags:

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?

like image 461
Paul Tarjan Avatar asked Aug 10 '09 21:08

Paul Tarjan


People also ask

How do you check if the image is completely loaded JavaScript?

Method 1: Using attributes of <img> to check whether an image is loaded or not. The attributes we will use are: onload: The onload event is triggered when an image is loaded and is executed.

How do you know when an image is fully loaded?

To determine whether an image has been completely loaded, you can use the HTMLImageElement interface's complete attribute. It returns true if the image has completely loaded and false otherwise. We can use this with naturalWidth or naturalHeight properties, which would return 0 when the image failed to load.

How do I check if a website is fully loaded in JavaScript?

After you have a basic understanding of javascript, you can detect when a page has loaded by using the window. onload event. window. onload = function() { addPageContents(); //example function call. }

Which event occurs when page image has been loaded?

The load event is fired when the whole page has loaded, including all dependent resources such as stylesheets and images.


1 Answers

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.

like image 173
redsquare Avatar answered Dec 04 '22 21:12

redsquare