PDA

View Full Version : jquery: to get image width on page ready


lauthiamkok
08-02-2009, 03:31 AM
Hi,
How can I get the width of an image when the page is ready...?

I tried the code below, but it always return the value '0'...!

<script type="text/javascript" src="js/jquery/jquery-1.3.1.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var width = $("#image img").width();
alert(width);
});
</script>

<div id="image">
<img src="xxx.jpg" />
</div>

many thanks,
Lau

rnd me
08-02-2009, 04:26 PM
ready fires before images load.
window.onload would work.

lauthiamkok
08-02-2009, 05:29 PM
ready fires before images load.
window.onload would work.

thanks, i got the answer now which is i need to put the size of the image in img tag so that javascript can pick up the value on page ready.

this is the page im working on. if u turn off javascript on your browser then u will see what i want to achieve to degrade the page gracefully when there is no javascript enabled.

http://lauthiamkok.net/tmp/projects/part/

thanks,
Lau :D