Solution: Javascript returns incorrect width or height of an element containing image(s) when page first loads
Recently, I’ve been doing quite a lot of JavaScript development, part of which involved equalising heights of a group of similar elements on the page. I thought this would be very simple and indeed it was but it threw up a JavaScript quirk which I have seen several times before. The solution is simple, but here’s what happens and why (in this example, my browser is Firefox 3.6.8 on Mac OSX):
As I mentioned, I was attempting to use JavaScript to measure the height of a group of elements, each of which contained an image and some text and were fixed-width. The JavaScript miscalculated the height of the elements when the page first loaded and also when I cleared my browser cache then reloaded the page but a normal refresh would result in the JavaScript returning the correct height for the elements. The reason this happens is a pretty obvious once you think about it in detail.
SInce I had been lazy and had not specified the dimensions of the images on my page, I was relying on the browser to render the images at their native size. My JavaScript was being run on “domready” (an event which most JavaScript frameworks such as Mootools and JQuery add under some guise – this fires when the DOM tree has been fully constructed but before images etc have been downloaded) – this is a vital factor.
Because I didn’t specify the dimensions of the images on my page, the browser had no way of determining what those dimensions should be until the images had been downloaded and analysed (this would occur after “domready”, at “load”). Once the images on my page had been downloaded and cached, the browser knew their dimensions even at “domready” and could account for the effect the images had on the dimensions of their container elements.
So, the solution is very simple, either specify the dimensions of your images, preferably using either inline or external styles (or by oldschool HTML attributes if you need to) or run your JavaScript on “load” rather than “domready”. The former (specifying the dimensions of your images) is most likely to be the best solution since it will give faster performance for your web page(s).
I found today through a bit of testing, that Firefox 3.6 and Opera 10 behave as above i.e. they report the container element dimensions correctly after the image has been cached. I then tested Safari 5 and found that it never reports the correct dimensions of the container element if the dimensions of the image(s) are not specified – this may be due to my cache settings in Safari.
You can see an example of this behaviour here – box1 should be reported with smaller height than box 2 on first load with Firefox and Opera and probably always with Safari, if you then refresh the page, both heights should be displayed correctly. I have used Mootools element.getSize() and element.getCoordinates() in the example which are triggered onDomReady.
NOTE: I’d expect the same behaviour to occur with widths if the width of your element is dependent on the width of an image it contains.
If you find any other behaviour with other browsers, i’d be interested to know so please leave me a comment below!
I hope that saves someone the time of tracking this bug through and explains why the apparently quirky behaviour is happening.
| This entry was posted by Neil Craig on August 20, 2010 at 6:34 pm, and is filed under (X)HTML, Browser bugs, General Web-Development, Javascript, MooTools. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
How to install a Java runtime on Apple Mac OSX Mountain Lion
about 10 months ago - 1 comment
One of the (perhaps) understandable but more irritating aspects of Apple Mac OSX upgrades which I just experienced is the fact that my upgrade from lion to mountain lion is that the Java runtime (JRE) is uninstalled…This meant that my usual code editor/IDE Netbeans was broken – not good! So i searched around and found More >
When to use an img tag, when to use a CSS background image
about 2 years ago - 3 comments
<Img> tag vs CSS background image…This has caused much confusion to many web-developers I have met – many more developers don’t care. So, when should you use an <img> and when should you use a CSS background image on your element? The answer is surprisingly simple…In nearly all cases, use <img> tags for contextually important More >
Mobile Website design & development guidelines
about 2 years ago - 1 comment
Like it or hate it, the Apple iPhone has ushered in the age of the “Smartphone” which in turn has delivered mobile web-browsing to the masses. The mobile browser, whilst now broadly quite capable (at least on leading platforms), has introduced a slightly different set of requirements in terms of design and development of Websites.
Many, many designers, developers and others have written articles on how to design and/or develop for the mobile browser. I am aiming to summarise what I have learnt from several sources alongside my experience an opinion in a (hopefully) relatively quick and simple way
How to reduce web page download time in 4 (fairly) simple steps
about 2 years ago - No comments
Web pages have generally become much more bulky in recent years. The trends for JavaScript-based functionality enhancements and the popularisation of digital photography have led to larger amounts of data being transferred (downloaded) per page and I can’t see a reversal of this happening any time soon. If you’re using a mobile device such as More >
How to create a cross-platform network controlled jukebox system (using VLC)
about 2 years ago - 2 comments
A simple way of setting up a music system (jukebox-style) which can be remote-controlled via a web-browser interface over a computer network.
wget: saving a file to a custom location
about 2 years ago - 3 comments
This will be a really quick post which will be obvious to unix sysadmin veterans but hopefully will be helpful to us mere mortals… wget is a linux/*nix command line tool which can download files (including entire websites if options are set correctly) and at first glance, from looking at the wget man page you’d More >
Mootools element.morph() example
about 2 years ago - 3 comments
A simple example of the time-saving element.morph() Mootools method which allows you to morph HTML elements from one property set to another.
Mootools element.getChildren() Vs element.getElements() – What’s the difference?
about 2 years ago - 1 comment
A really quick post detailing the main differences between the very useful Mootools Javascript element.getChildren() and element.getElements() methods.
CSS Basics: The difference between outline and border
about 2 years ago - 3 comments
CSS outline and border are at a glance very similar but have a few important differences: Outline is effectively overlaid over the top of the element it is applied to and therefore outline does not add to the element width whereas border does add to the element width Outline (according to the official W3C specification More >
Flushing your DNS cache on Mac OSX 10.4, 10.5 and 10.6
about 3 years ago - No comments
If like me, you have to transfer websites between servers from time to time and you use an Apple Mac, you’ll probably need to know how to flush your DNS on Mac OSX (flushing is the term used for removing of all records to force an update). For all versions of OSX, the simplest way More >

about 2 years ago
What a great resource!
about 2 years ago
I am having trouble seeing my website the way it appears to others using the same browsers ie. safari omniweb. It only appears strange (with extra grey area around images/frames) on my iMac intel core duo. I am not savvy with code etc. can you advise how to fix this? Also I like the righthand sidebar boxes you have and would like to create similar clean style search tools on my site can you tell me how?
about 2 years ago
Hi Inga
I am not 100% sure what the problem is that you’re having but from a quick look at your website, it could be that you need to look at the floats on your images and their containers or possibly you might need to set padding to zero on images inside anchors .
Hope that helps
about 2 years ago
You made some good points there. I did a search on the subject and hardly got any specific details on other sites, but then great to be here, seriously, thanks.
- Lucas
about 2 years ago
well written blog. Im glad that I could find more info on this. thanks
about 2 years ago
thanks for the post
about 1 year ago
lol, simple solution for a very frustrating problem… Thanks
about 1 year ago
Dag nabbit good stuff you whipprensppaers!