My colleague encountered such a problem this morning.
He had to display a set of images which were retrieved from a co-partner's search engine's results. But the engine didn't supply the images sizes together with the results. So he had to decide what sizes should he use for displaying these images.
Before these images onloaded in the user's browser, their sizes were unknown and might vary a lot, which it was not what we expected. 'Cause the layout of the page might be in a mess for these undecided images sizes. So my colleague decide to resize these images sizes by registering an onload event handler for each image. But it has defects. The page will flash once when these images have loaded and the onload event handlers have triggered.
So I think there's might be two solutions.
One, show a default image which the size is fixed at first and hide the onloading images, when imags loaded, replace these defaults images with the onloaded images(for these images now have sizes, and we can calculate new proper sizes for displaying on retaining the original scale of the width and height).
Two, set the images size to fixed values before they loaded. When these images have loaded completely, the images size won't change. But the scale of the width and height might be not correct. So calculate the sizes with both fixed width/height and the original width/height of the image for a new proper one to display.
They decide to take the second solution for some reason. But my colleage find it was hard to retrieve the original sizes of those images after we specified a fixed width/height. So how we deal with that? Actually, after some "exploring", I found the answer it's simple. Remove the fixed sizes (or say the width/height attributes) we've set, and then retrieve them one more time. And we got the actual size of these images.
For instance, we may use something like the codes below:
img.removeAttribute('width');
img.removeAttribute('height');
or
img.style.width = '';
img.style.height = '';
Then we can get the original sizes of image instead the ones we set it before.
Which one to use depends on what method you've chosen for setting the images sizes.
Forgive my poor English representing . I don't know clearly what I'm saying either. -___-#
