I am observing an unusual behavior. I have a floating dialogue box in which I am placing an image. I want to get the image size using jquery and resize its div container to the appropriate size. It works fairly well, but the function occasionally returns zeros as the image sizes.
$('#dialogueContainer').html('<div class="dialogue"><img src="/photos/' + file + '" id="lightImage" /></div>');
var imageHeight = $('#lightImage').height();
var imageWidth = $('#lightImage').width();
console.log(imageHeight + 'x' + imageWidth); //<-- This occasionally returns "0x0"
I suspect that the image may not be ready in the DOM when jquery attempts to measure its height and width. Any thoughts?
You're right, the image is not loaded.
Worst, in IE, sometimes the size reported is something like 40x60 (the small icon you see when the image is not loaded yet).
jQuery reports that the load event can be used with images: http://api.jquery.com/load-event/
I tried a long time ago to solve this problem, cross browser, and I ended up polling the image sizes to trigger a custom "load" event.
Thanks
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With