Click to See Complete Forum and Search --> : image width and height problems


chrisjmeagher
08-02-2003, 11:46 AM
Hi. Here's the deal - I have a standard body template that I use through a .js file, with each HTML line document.writeln()'ed in. Part of this body template is an image, and I want the <IMG> tag to automatically detect the width and height of the image. So, I preload the image on the actual HTML page by placing var body_image = new Image(); and body_image.src = somepic.jpg; within the HEAD tags. However, I have problems in that sometimes the images load with 0 width and 0 height. And, this is an intermittent problem - on some computers all of the images load just fine. The image will appear with correct width and height if I refresh or reload the page, so I'm guessing that it's a problem with my preloading. Also, I tried a while loop on making sure that neither the width or height are 0, but that just makes the page take FOREVER to load.

So...are there any better ways to ensure that the width and height that the body script reads is non-zero - any better way to preload? Or, am I just being anal retentive about insisting that there be WIDTH and HEIGHT qualifiers on the <IMG> tag? If I left these out I know the images would load correctly but it's not as complete a solution.

- Chris

AdamGundry
08-02-2003, 11:49 AM
You've got bigger problems that image sizing if your pages are dependant on Javascript to work. If you want to use a template system, do it on the server side or not at all, otherwise the 13% or so of web users who have Javascript disabled won't be able to access your content.

Adam

chrisjmeagher
08-02-2003, 01:59 PM
I was worried about that. Would something like ASP be a good fix for browser compatability? Can you recommend any other server-side scripting languages?

Also...this is a small, personal website (albeit with thousands of pictures), so if anyone can suggest a javascript fix (however temporary until I learn server-side scripting) for the problem described in my first post I would be very grateful.

boojum
08-02-2003, 02:57 PM
do you specify width and height for that image anywhere?

chrisjmeagher
08-02-2003, 03:54 PM
Well...no. After preloading the image within the head tags, I use body_image.height and body_image.width to specify the HEIGHT and WIDTH values for the<IMG> tag in my body, but I run into problems now and then as those values are sometimes read as zeroes.

boojum
08-02-2003, 04:08 PM
i dont know what "Part of this body template is an image, and I want the <IMG> tag to automatically detect the width and height of the image." means, but try body_image.style.height='NUMBER%'. or 'NUMBERpx' im not sure what you want.

chrisjmeagher
08-02-2003, 06:46 PM
Sorry for the confused wording. On my actual HTML pages, I preload the images. In the body of those pages, I call a javascript with the following line:
document.writeln('<IMG SRC="' + body_image.src + '" WIDTH="' + body_image.width + '" HEIGHT="' + body_image.height + '" BORDER="0" NAME="body_image">

...in the hope that by preloading the image the width and height can be autodetected.

Khalid Ali
08-02-2003, 10:31 PM
yes if the image is preloaded then you should be able to get the width and height..
I am thinking u are trying to get those properties before they are completely loaded.