Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine if an image has loaded, using Javascript/jQuery?

I'm writing some Javascript to resize the large image to fit into the user's browser window. (I don't control the size of the source images unfortunately.)

So something like this would be in the HTML:

<img id="photo"      src="a_really_big_file.jpg"      alt="this is some alt text"      title="this is some title text" /> 

Is there a way for me to determine if the src image in an img tag has been downloaded?

I need this because I'm running into a problem if $(document).ready() is executed before the browser has loaded the image. $("#photo").width() and $("#photo").height() will return the size of the placeholder (the alt text). In my case this is something like 134 x 20.

Right now I'm just checking if the photo's height is less than 150, and assuming that if so it is just alt text. But this is quite a hack, and it would break if a photo is less than 150 pixels tall (not likely in my particular case), or if the alt text is more than 150 pixels tall (could possibly happen on a small browser window).


Edit: For anyone wanting to see the code:

$(function() {   var REAL_WIDTH = $("#photo").width();   var REAL_HEIGHT = $("#photo").height();    $(window).resize(adjust_photo_size);   adjust_photo_size();    function adjust_photo_size()   {     if(REAL_HEIGHT < 150)     {       REAL_WIDTH = $("#photo").width();       REAL_HEIGHT = $("#photo").height();       if(REAL_HEIGHT < 150)       {         //image not loaded.. try again in a quarter-second         setTimeout(adjust_photo_size, 250);         return;       }     }      var new_width = . . . ;     var new_height = . . . ;      $("#photo").width(Math.round(new_width));     $("#photo").height(Math.round(new_height));   }  }); 

Update: Thanks for the suggestions. There is a risk of the event not being fired if I set a callback for the $("#photo").load event, so I have defined an onLoad event directly on the image tag. For the record, here is the code I ended up going with:

<img id="photo"      onload="photoLoaded();"      src="a_really_big_file.jpg"      alt="this is some alt text"      title="this is some title text" /> 

Then in Javascript:

//This must be outside $() because it may get called first var isPhotoLoaded = false; function photoLoaded() {   isPhotoLoaded = true; }  $(function() {   //Hides scrollbars, so we can resize properly.  Set with JS instead of   //  CSS so that page doesn't break with JS disabled.   $("body").css("overflow", "hidden");    var REAL_WIDTH = -1;   var REAL_HEIGHT = -1;    $(window).resize(adjust_photo_size);   adjust_photo_size();    function adjust_photo_size()   {     if(!isPhotoLoaded)     {       //image not loaded.. try again in a quarter-second       setTimeout(adjust_photo_size, 250);       return;     }     else if(REAL_WIDTH < 0)     {       //first time in this function since photo loaded       REAL_WIDTH = $("#photo").width();       REAL_HEIGHT = $("#photo").height();     }      var new_width = . . . ;     var new_height = . . . ;      $("#photo").width(Math.round(new_width));     $("#photo").height(Math.round(new_height));   }  }); 
like image 863
Kip Avatar asked Nov 04 '08 20:11

Kip


People also ask

How do you know if an image has loaded JavaScript?

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 you check if the document is loaded in jQuery?

Detect if Document has loaded properly using JSvar interval = setInterval(function() { if(document. readyState === 'complete') { clearInterval(interval); init(); } }, 100);


2 Answers

Either add an event listener, or have the image announce itself with onload. Then figure out the dimensions from there.

<img id="photo"      onload='loaded(this.id)'      src="a_really_big_file.jpg"      alt="this is some alt text"      title="this is some title text" /> 
like image 82
Diodeus - James MacFarlane Avatar answered Oct 12 '22 01:10

Diodeus - James MacFarlane


Using the jquery data store you can define a 'loaded' state.

<img id="myimage" onload="$(this).data('loaded', 'loaded');" src="lolcats.jpg" /> 

Then elsewhere you can do:

if ($('#myimage').data('loaded')) {     // loaded, so do stuff } 
like image 40
Mike Fogel Avatar answered Oct 12 '22 03:10

Mike Fogel