Link to home
Start Free TrialLog in
Avatar of raterus
raterusFlag for United States of America

asked on

Preloading images, and getting width/height

I'm have a need when my page loads to a) Load an image and then b) get the width/height.  The problem I'm running into, at least in firefox, is when the image is first loaded, the width/height are reported incorrectly.  However, if I refresh the page, and the browser loads from cache, the correct width/heights are recorded.

Here's what I'm doing

var fppImage = $('<img />');
fppImage.attr('src', '/some/image/url.jpg').appendTo('body');

var iH = fppImage.height();
var iW = fppImage.width();

alert(iW + " " + iH);  

Open in new window


iW & iH return 24px on the first page load, but subsequent page loads return the correct dimensions of the image

It seems pretty straightforward, but doesn't work as you'd expect in firefox.  Can anyone suggest some ideas to actually preload the image?
SOLUTION
Avatar of Md Shah
Md Shah
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial