BrettM
11-02-2006, 12:49 AM
I have a simple setup with two divs that are position:absolute. The first div is a header that I allow to expand to accomodate long topic titles or narrow browser windows that cause the text to wrap. The second div is the content, which scrolls beneath the header. When the header resizes, the content div must be repositioned so the top is below the header. In IE6 I also have to reset the height of the content div, or the vertical scroll bar extends below the bottom of the window. Here is my script as it currently stands:
<script type="text/javascript">
window.onload = adjustContent;
window.onresize = adjustContent;
var DHTML = (document.getElementById || document.all);
function adjustContent() {
if (!DHTML) return;
var oHeader = new getObj('FPheader');
var oContent = new getObj('FPcontent');
oContent.style.top = oHeader.obj.offsetHeight + "px";
oContent.style.height = document.body.offsetHeight - oHeader.obj.offsetHeight;
}
function getObj(name){
if (document.getElementById){
this.obj = document.getElementById(name);
this.style = document.getElementById(name).style;
}
else if (document.all){
this.obj = document.all[name];
this.style = document.all[name].style;
}
}
</script>
This script works perfectly in Netscape 7-8, Opera 8-9, Firefox 2, and IE6. Which is kind of amazing, because document.body.offsetHeight is 0 to all the browsers except IE6, so the script is attempting to set a negative style.height. Without the '+"px"' addition to the header height, the script will not work in Netscape7-8 or Firefox. Opera and IE6 don't care.
This morning I installed IE7, and things went bad fast. It doesn't like the "px", and it doesn't like the negative height. It's also showing a double vertical scroll bar, suggesting that it wants a width set, but the scroll bar is the right height, unlike IE6.
Is there any possible way I can fix this script so that it will work without browser sniffing? It seems so elegant the way it is, if I can just figure out what to use in place of the body's offsetHeight and what to do about the "px" problem. It is essential that it work in IE6-7, since it's a template for an HTML help project, but I would like it to be fully cross-browser compatible if possible so the project could be exported as a Web site.
Thanks in advance.
Brett
<script type="text/javascript">
window.onload = adjustContent;
window.onresize = adjustContent;
var DHTML = (document.getElementById || document.all);
function adjustContent() {
if (!DHTML) return;
var oHeader = new getObj('FPheader');
var oContent = new getObj('FPcontent');
oContent.style.top = oHeader.obj.offsetHeight + "px";
oContent.style.height = document.body.offsetHeight - oHeader.obj.offsetHeight;
}
function getObj(name){
if (document.getElementById){
this.obj = document.getElementById(name);
this.style = document.getElementById(name).style;
}
else if (document.all){
this.obj = document.all[name];
this.style = document.all[name].style;
}
}
</script>
This script works perfectly in Netscape 7-8, Opera 8-9, Firefox 2, and IE6. Which is kind of amazing, because document.body.offsetHeight is 0 to all the browsers except IE6, so the script is attempting to set a negative style.height. Without the '+"px"' addition to the header height, the script will not work in Netscape7-8 or Firefox. Opera and IE6 don't care.
This morning I installed IE7, and things went bad fast. It doesn't like the "px", and it doesn't like the negative height. It's also showing a double vertical scroll bar, suggesting that it wants a width set, but the scroll bar is the right height, unlike IE6.
Is there any possible way I can fix this script so that it will work without browser sniffing? It seems so elegant the way it is, if I can just figure out what to use in place of the body's offsetHeight and what to do about the "px" problem. It is essential that it work in IE6-7, since it's a template for an HTML help project, but I would like it to be fully cross-browser compatible if possible so the project could be exported as a Web site.
Thanks in advance.
Brett