tags:

views:

208

answers:

2

Hey guys,

I just got a problem which is I can't get the height property of a <p>.(use javascript document.getElementById(id).offsetHeight)

It works fine both in FF or chrome.

I have tried to set the style of the <p> to height:100%;/height:auto;

but both not work.

Please help me, thanks :D

UPDATE: It seems that IE6 didn't return the offsetHeight value when page is loading, so maybe I have to put the javascript in the onload event? (Cause I put the javascript in the html during the page is load before).

+1  A: 

Try document.getElementById(id).style.pixelHeight (elm.style.pixelHeight/Width is an IE-only property.) I've found sometimes it works but not offsetHeight for some reason.

See also http://msdn.microsoft.com/en-us/library/ms531127(VS.85).aspx

It may also pay to add a "zoom: 1" to the element's style to trigger hasLayout, which can sometimes fix finding the height if I recall correctly.

David Morrissey
pixelHeight doesn't work.I have read some articles that said the ie6 only returns offsetHeight value when the page is fully loaded.
is it returning `0/null`, or raising an exception? If it's the latter, then not being fully loaded is probably entirely plausible.
David Morrissey
bear in mind of course that IE raise exceptions if the elements have a `display` of `none` where other browsers return `0` though if that's relevant
David Morrissey
A: 

The answer is IE6 didn't return the offsetHeight value when page is loading, so I have to put the javascript in the body onload value.

It works.

Thanks for all the replies.

I think this time the fundamental issue is not within IE but rather in fact that before onload there is no guarantee the document is completely built AND RENDERED at all. Once it may work, on another reload (or on another day when server is busy and pauses between sending chunks of data of a single resource/document) your call may end with an error. Also even when your element might be already created and maybe even rendered when you access its visual properties its children may be still waiting on some rendering queue and when they get rendered your integer value becomes outdated.
Jaroslav Záruba
What I'm trying to say is that in general it is always safer to start with onload, because then you know that the document is actually ready.
Jaroslav Záruba