michael.futreal.com > jQuery > ready vs. onload > example 2

Image Size Computed at load Time

In Firefox 2.0 (and hopefully all other modern browsers), you should find that the sentence below reports a correct height of 100 pixels. The same test fails in Firefox 2.0 when the usual jQuery based $(document).ready() trigger for retrieving the height is used. The difference occurs because of issues with when and how .ready() is triggered.

The Test

The image below is y pixels tall.

Saturn

How This Page Does It

This page utilizes David Flanagan's runOnLoad function as an alternative mechanism to the usual $(document).ready() convention used in jQuery.


runOnLoad(function(){
   $("#sizeReport").html( $("#saturnID").height());
});

runOnLoad performs basically the same task as jquery's DOM-ready mechanism, $(function): it queues up all the functions supplied and runs them when the document is available. The differences is that $(function) runs before images have completely downloaded and runOnLoad waits until the image downloads are complete.

I don't recommend that you mix the runOnLoad and $(document).ready() metaphors. Pick one model or the other, depending on whether or not this underspecified <img> thing is a problem for your site.