image size problem

X

xdevel1999

Hi,

I have the following problem:

the FIRST time I load a page with Firefox 3, always with Safari/Chrome
and never with Opera/IE7

the image.width and heigth are not filled but they have a 0 pixel size

Ex.

i = new Image()
i.src = "image.png"

// here the problem
i.width
i.height

are 0

May be is a loading problem? I try to get that values before the
images are fully loaded?

How can I solve that?

Thanks
 
E

Erwin Moller

xdevel1999 schreef:
Hi,

I have the following problem:

the FIRST time I load a page with Firefox 3, always with Safari/Chrome
and never with Opera/IE7

the image.width and heigth are not filled but they have a 0 pixel size

Ex.

i = new Image()
i.src = "image.png"

// here the problem
i.width
i.height

are 0

May be is a loading problem? I try to get that values before the
images are fully loaded?

Makes sense yes.
I am not sure, but maybe getting the height and width wait with their
response untill the image is loaded?

Anyway: You could test if the image is loaded before trying to read its
dimensions.
You can use the complete property for this (returns boolean)

Propably it makes more sense to attach the 'onload' eventhandler to the
image.
i.onload = yourfunctionhere

Your function 'yourfunctionhere' will be called when the image is 100% in.

Regards,
Erwin Moller
How can I solve that?

Thanks


--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
 
X

xdevel1999

Sorry for the late...
You can use the complete property for this (returns boolean)
Propably it makes more sense to attach the 'onload' eventhandler to the
image.
i.onload = yourfunctionhere

yes both are good but the problem is that if I do:

function()
{
img.onload = fun() { dosomething() }

a = img.width;
b =img.height;
}

above the code of getting img properties fails because it is executed
before of the onload image function that it does not completed yet
I don't want put a timer bacause the images can be a lot

Thanks
 
D

David Mark

Sorry for the late...


yes both are good but the problem is that if I do:

function()
{
   img.onload = fun() { dosomething() }

Fix the typo and put the following two lines in this function.
   a = img.width;
   b =img.height;

}

[snip]
 
X

xdevel1999

E

Erwin Moller

xdevel1999 schreef:
Sorry for the late...


yes both are good but the problem is that if I do:

function()
{
img.onload = fun() { dosomething() }

a = img.width;
b =img.height;
}

above the code of getting img properties fails because it is executed
before of the onload image function that it does not completed yet
I don't want put a timer bacause the images can be a lot

You approach it wrong.
Don't ask for the weight and height UNTILL your function coupled to
'onload' is called.

You are asking JavaScript to give you the width and height of images
that have not arrived yet.


--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
 

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,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top