Setting position to absolute as Dan recommends accomplishes taking the
element out of the document flow so surrounding elements are not very
momentarily shifted when the display style changes from hidden to
block. Setting visibility to hidden doesn't take the element out of
the document flow and risks the very minimal chance the user sees a
flicker when display is set to block.
The sample below demonstrates this. In FF3, IE7, Safari and Chrome,
setting position to absolute will take the div out of the document
flow and offsetHeight returns the same value regardless if position is
set to absolute. You can see the difference setting position to
absolute makes by running the code once with "d.style.position =
'absolute'" commented out, and running it again with that line
uncommented.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
function checkHeight() {
var d = document.getElementById('divThis');
// uncomment line below so bottom div is not
// shifted during first alert
//d.style.position = 'absolute';
d.style.visibility = 'hidden';
d.style.display = '';
var iHeight = d.offsetHeight;
alert('intentional pause ...');
d.style.display = 'none';
d.style.visibility = 'visible';
d.style.position = '';
alert(iHeight);
return false;
}
</script>
</head>
<body>
<div style="height:50px;background-color:red;">
stuff ...
<input type="button" onclick="return checkHeight()"
value="Check Height" />
</div>
<div id="divThis" style="background-color:green;display:none;">
test<br />
test<br />
test<br />
test<br />
</div>
<div style="background-color
range;">
more stuff ...
</div>
</body>
</html>