To ensure that all images are loaded completely, I use the onImagesLoad plugin. This, so far, works just fine on all browsers.

However, on IE8 (and I assume other versions of IE also) when I then iterate over the img elements, I am unable to determine the width/height of the images loaded. The image.context.naturalWidth and naturalHeight attributes don't seem to work.

How do I get a hold of the images' dimension?

Thanks heaps :)

Update

@Simon: That didn't work on IE, and broke the other browsers as well.

@Jenechka: If "imageDomElement" is just another name for the "image" variable in my example above, then it doesn't work. Or what do you mean by that DomElement?

7 Answers
7

It's been a while but I finally found some time to tinker with this again. The above problems are still there, but here is what I think is going on.

When I load the initial images then yes, the file is loaded and image objects are generated. But it seems that the attributes are not correct yet, and they won't be until the image is actually added to the DOM of the site and rendered. A div/image on hide() on IE has no dimension information whatsoever, on Safari there is some information available. For example, without adding the following div anywhere

var test = $("<div><img src='test.jpg'></div>")

the image contained there has the following information:

width() = 0,

attr("width") = 600,

css("width") = "", and

img[0].clientWidth = 0.

That's on Safari; on IE it's the same except attr("width") = 0 and css("width") = "auto". Now I can't use this, and that's what broke my script and why I posted my initial question. However, the moment I append this div and have it rendered, all the correct values show up in the image object.

I'm writing a little gallery thinghie, which shows whatever images I have in that second .html file that I load; that gallery, however, computes and places the thumbnails, and prepares the images it shows in full resolution. To make this look ok, I basically wanted to create the entire thing hidden, and then fade it in. Alas, it seems that this whole idea won't pan out. A friend suggested to load everything into a tiny iframe off to the left where it's not visible, and work with that. Perhaps that's the way to go.

Another thing I noticed, and that seems to be very closely related to the aforementioned load issue is clone(). It seems that if an image is rendered, a

var newimg = img.clone()

generates the same "empty" image object that I have to deal above. Even when the original image is visible and contains all the right attributes, its clone does not.

Right now I don't see any other way than to rethink and rewrite parts of my gallery.

Thanks for the link, but the code there doesn't reflect the scenario. I don't embed the <img> elements into the <div> but instead load() a list of <img> elements from an external .html file. I suspect that the additional loading of image elements, embedding them into the DOM, and invoking a callback upon "image ready" (whatever that means for a browser!) causes the issues I see on different browsers.
–
JensJun 5 '10 at 23:55