Jump to content

JavaScript failing to detect image height and width


tinwatchman

Recommended Posts

So I have a script that's supposed to detect the height and width of an image when said properties have been set in the HTML. Where imgList is a list of object references to a group of Images: for (i in imgList) { var currImg = imgList; var w = currImg.width; var h = currImg.height; alert(currImg.alt + ": width = " + w + "; height = " + h); }Here's the issue: whenever I run the program, w and h return as 0. Even though the width and height have been set in the HTML! As below:<img src="images/photo1.jpg" alt="Photo 1" width="128" height="96" />Height and width return 0 even when I change the alert statement in the above function to the following:alert(currImg.alt + ": width = " + currImg.width + "; height = " + currImg.height);What's really driving me nuts here is that every single time, JavaScript has successfully detected the correct "alt" property for the image -- but not the correct height and width! I'm ready to tear my hair out here. Can anyone help me out here?Thanks!

Link to comment
Share on other sites

Are you trying to access this info in the open script or in a function? While the page is loading or after the page has loaded? Have the images themselves finished loading? Depending on the browser, all this stuff might make a difference.

Link to comment
Share on other sites

Well, for the benefit of anyone else who runs into this, I figured it out. My problem was, basically, that I was testing this in IE6. As far as I can tell, this is an error that only comes up in that browser -- but not in IE7 or Firefox. I was able to get around this by getting the innerHTML of the div surrounding the image and parsing that for the height and width information. Not a whole lot of fun, just to support one browser.To answer your questions briefly, though:Are you trying to access this info in the open script or in a function?A function located in an inline script in the <head> section of the page.While the page is loading or after the page has loaded?After; the function is called by the onload event in the <body> tag.Have the images themselves finished loading?Yes. At one point, I put in an alert() which reported the state of the complete property of the image. It came back true.Depending on the browser, all this stuff might make a difference.:) Turns out that it did. Thanks for replying.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...