delerious
12-16-2003, 05:45 AM
I've read that IE 5/5.5 stores elements such as scrollLeft and scrollTop in the document.body object, and IE6 stores those elements in the document.documentElement object.
I wrote up the following javascript function:
function getScrollTop() {
var s = 0;
if (document.documentElement && typeof(document.documentElement.scrollTop)
!= 'undefined') {
s = document.documentElement.scrollTop;
}
else if (document.body && typeof(document.body.scrollTop) != 'undefined') {
s = document.body.scrollTop;
}
return s;
}
But that didn't work as document.documentElement.scrollTop is actually defined in IE 5.5 (it always returns 0 though).
So I rewrote it like this:
function getScrollTop() {
var s = 0;
if (document.documentElement && document.documentElement.scrollTop) {
s = document.documentElement.scrollTop;
}
else if (document.body && document.body.scrollTop) {
s = document.body.scrollTop;
}
return s;
}
That works, but if the page hasn't scrolled at all, then it is only working by accident. If the page hasn't scrolled at all, the code within both the if clause and the else-if clause are not executed, because the scrollTop has a number value of 0, and javascript treats a 0 number value as false. So the
function isn't really working correctly, but it is returning the correct value since the s variable is initialized to 0. If the s variable were initialized to 10, then the function would return the wrong value.
I am wondering how I can get IE to go into the proper if/else-if clause.
I wrote up the following javascript function:
function getScrollTop() {
var s = 0;
if (document.documentElement && typeof(document.documentElement.scrollTop)
!= 'undefined') {
s = document.documentElement.scrollTop;
}
else if (document.body && typeof(document.body.scrollTop) != 'undefined') {
s = document.body.scrollTop;
}
return s;
}
But that didn't work as document.documentElement.scrollTop is actually defined in IE 5.5 (it always returns 0 though).
So I rewrote it like this:
function getScrollTop() {
var s = 0;
if (document.documentElement && document.documentElement.scrollTop) {
s = document.documentElement.scrollTop;
}
else if (document.body && document.body.scrollTop) {
s = document.body.scrollTop;
}
return s;
}
That works, but if the page hasn't scrolled at all, then it is only working by accident. If the page hasn't scrolled at all, the code within both the if clause and the else-if clause are not executed, because the scrollTop has a number value of 0, and javascript treats a 0 number value as false. So the
function isn't really working correctly, but it is returning the correct value since the s variable is initialized to 0. If the s variable were initialized to 10, then the function would return the wrong value.
I am wondering how I can get IE to go into the proper if/else-if clause.