views:

109

answers:

5

I need to have a function called after a larger image is loaded on the page. I've tried some various solutions that I've found on here and around the web, but none either fit or work. Here's what I've tried that seems to make the most sense...and this is using jQuery

var imgs = $('#bgStage img.bg'),
        imgCnt = imgs.length, cnt = 0;

    imgs.load(function(){
        cnt++;
        if(imgCnt === cnt){
            homeSlider();
        }
    }).each(function(){
        if(this.complete){
            $(this).trigger('load');
        }
    });
}

This doesn't seem to wait until the img.bg is loaded. The homeSlider() function is called and started as I still see the image still loading.

So, I am wondering how a browser determines an image is loaded? Is it when it can read the width and height? Because I am defining the width and height in the CSS for the image to force it to be a certain size.

If anyone has any insight as to what makes the onload event fire for an image, that'd be great! Thanks.

A: 

You can always check for $('img').length();

Wes
Do you mean $('img').length; ? I don't need to know how many images there are. I checked docs.jquery.com and I don't see a length() method, is there a length() method I am not seeing or know about that you are referring to?
Jeff
My Bad, you're right Jeff.
Wes
A: 

Here's a sample code that should work. I did a project that needed this and I remember that some problems might include:

  • The browser caches the image (IE i believe) so I had to append ?[random] at the end
  • Maybe you have to set the src javascriptly so that your event is hooked at the right time

Sample code:

<!DOCTYPE html>
<html>
<head>
<title>Sample</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
<script type="text/javascript">
$(function(){       
    $('#imageSample')
        .load(function(){
            alert($('#imageSample').width());
            alert($('#imageSample').height());
        })
       .attr('src', 'http://www.gimp.org/tutorials/Lite_Quickies/quintet_hst_big.jpg?' + new Date());

});
</script>
<style type="text/css"></style>
</head>
<body>
    <img id="imageSample" src="" alt="" />
</body>
</html>
Dan
The src is being supplied via CMS, so adding it through javascript is going to be a bit of a hassle.
Jeff
It should work also even if you don't set the src. At least on chrome and ff.
Dan
Using .load() method I get the same results. The function I am called doesn't wait for the image to fully load.
Jeff
what browser are you using? here's the doc for load (http://api.jquery.com/load-event/), it should work on chrome and firefox. I didn't test on IE but I think it should work. You can also check the code of lightbox2, it loads the image then waits to get the height and width.
Dan
A: 

Images might be cached, try this: http://github.com/peol/jquery.imgloaded/raw/master/ahpi.imgload.js

vls
Happens even if I clear my temp internet files/history/cache the whole thing, still gives me the problem.
Jeff
A: 

Use something simple like so:

var loaded =  false;
var len = $('#bgStage img.bg'), c = 0;
$('#bgStage img.bg').each(function(){

    $(this).attr('src',$(this).attr('src') + new Date()); //Remove caching.

    $(this).bind('onload',function(){

        //Other Stuff here!

        if(c == len)
        {
            HomeSlider();
        }
        c++; //Increment
    });
});

Tell me how it goes :)

RobertPitt
Tried something like this, still fires before the image has completely loaded.
Jeff
you tried using the `onload` event ? Updated!
RobertPitt
Yes, tried the onload even and I get the same results.
Jeff
A: 

Each image has a complete property. Unfortunately not all browsers support it. They always report true, even if the image hasn't loaded at all. IE gets it right. ;-)

http://simon.html5.org/test/html/semantics/img/src/ (not mine)

DanMan