Javascript question

CubanCorona

Senior member
Jul 13, 2001
258
0
0
I have a webpage with an image on it. The image is dynamically loaded using ASP. In order to preserve the layout of my page, I must make sure that the image width is less than 500 pixels. If it is not, I need to set <img width='500'.

I wrote a small segment of code that checks the image width and resizes it if it is greater than 500. This code is at the end of the page. However, large images that take a long time to load are not resized because they are not finished loading when the code segment is run. Is there any way I can make this work regardless of how long it takes the image to load? I tried the onload event of img tag, but it doesnt seem to work... my syntax could be wrong though.

Any javascript gurus out there have a solution?
 

hellman69

Member
Feb 15, 2003
180
0
71
This should do it. The idea is to have it recall itself if it can't determine the width.

function check_size() {
var img_width = "";
// Your javascript here that assigns the width to a variable
if (img_width == "") { setTimeout("check_size()",500); }
}


Trevor
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
This is the code from the site in my sig to keep images from being larger than the browser window. They load at the correct width, and don't resize when done laoding. (example page) Modify as neccesary.