image onload fires too early.

D

dax

This is a script to scale thumbnails depending on the size specified
by an application setting. I've dummied down the script to make it a
little more understandable.

I think I'm getting closer to solving this, but it appears that in IE
the onload event fires too early when used on an image.

Here is my script.

function scaleImage(imageId, size)
{

var pic=document.getElementById(imageId);
var w=pic.width;
var h=pic.height;

if (h > w)
{
if (h > size)
{
var f = h / size;
var newwidth=w / f;
var newheight=h / f;
}
else
{
var newwidth=w;
var newheight=h;
}
}
else if (w >= h)
{

if (w > size)
{
var f = w / Size;
var newwidth=w / f;
var newheight=h / f;
}
else
{
var newwidth=w;
var newheight=h;
}
}

pic.display = 'visible';
pic.height = newheight;
pic.width = newwidth;

}

Images are loaded such as below.

<img src="image1.gif" id="image1"
onload="ScaleImage('image1','150')">
<img src="image2.gif" id="image2"
onload="ScaleImage('image2','150')">
<img src="image3.gif" id="image3"
onload="ScaleImage('image3','150')">
<img src="image4.gif" id="image4"
onload="ScaleImage('image4','150')">
etc.....

What happens is inconsitantly when the images are loaded, the onload
event triggers too early and the height and width attributes are
registered as 0.

Is there a way to force the onload event to take place after the image
has loaded?

Thanks

dax
 
T

Thomas 'PointedEars' Lahn

dax said:
[...]
I think I'm getting closer to solving this, but it appears that in IE
the onload event fires too early when used on an image.

Here is my script.

[...]
<img src="image1.gif" id="image1"
onload="ScaleImage('image1','150')">
<img src="image2.gif" id="image2"
onload="ScaleImage('image2','150')">
<img src="image3.gif" id="image3"
onload="ScaleImage('image3','150')">
<img src="image4.gif" id="image4"
onload="ScaleImage('image4','150')">
etc.....

`img' elements have no `onload' attribute in Valid (X)HTML. Use the
`onload' attribute of the `body' element instead with which you stand
the best chance of getting consistent results.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top