Forum Moderators: open

Message Too Old, No Replies

capture height of div in IE

capture height of div in IE

         

user9

5:33 pm on Sep 7, 2004 (gmt 0)

10+ Year Member



Hi everyone

I have a problem with determining the exact height of div in IE.

The div in CSS looks like this


#main
{
width:200px;
}

I'm referencing it in the following way with JS


....
temp=document.getElementById('main');
alert(temp.currentStyle.height);
.....

alert will simply return the value of "auto", but I need the exact pixel size of the layer as it grows or shrinks, depending on the content size. I have no problem accomplishing this with a similar function in Mozilla, but I can't figure out how to get the pixels in IE.

Thanks for your help

Bernard Marx

5:46 pm on Sep 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



temp.offsetHeight

Cochrane

5:54 pm on Sep 7, 2004 (gmt 0)

10+ Year Member



User9:
As far as I know, if you specify a CSS width, but NOT a height, then the height style property value will always be auto. You might want to try this:

#main
{
width:200px;
height:350px;
overflow:auto;
}

This should make the Div scroll when the content is longer than 350px. I hope this helps.

Bernard Marx

6:26 pm on Sep 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Then perhaps we should try it:
[pre]

<html>
<head>
<style>
#test{width:150px;height:100px;border: solid 1px gray;}
</style>
<script>

window.onload = function()
{
var test = document.getElementById("test");
alert("before: "+ test.offsetHeight);
test.innerHTML = "Blah blah blah blah blah blah blah "
+ "blah blah blah blah blah blah blah "
+ "blah blah blah blah blah blah blah ";
alert("after: "+ test.offsetHeight);
}

</script>
</Head>
<body>
<div id="test">
</div>
</Body>
</HTML>[/pre]

Testing the local style object won't give the actual height, if the element has been stretched by content.

user9

6:39 pm on Sep 7, 2004 (gmt 0)

10+ Year Member



Thank you very much for your replies Bernard Marx and
Cochrane.

offsetHeight worked very well. I also tried your solution, Cochrane. The overflow method provides a nice alternative to what I was trying to do.

Thanks again,

user9

[edited by: user9 at 6:47 pm (utc) on Sep. 7, 2004]

Noisehag

6:40 pm on Sep 7, 2004 (gmt 0)

10+ Year Member



Nice test Bernard. I use a similar script to dynamically build foreground image borders on sites that my clients insist upon being fully printable by the end-user. Seems to work well on all the latest browsers and of course there are still the repeating background images if their browser does not support it or they have javascript turned off.