After much searching and playing with code, I’ve come to find this method to be most effective cross-browser solution to get and set the browsers scrollbar position. It has been tested to work in IE6, IE7, Firefox, Opera, and assumed to work in other popular browsers, as well.
function getScrollXY() {
var x = 0, y = 0;
if( typeof( window.pageYOffset ) == 'number' ) {
// Netscape
x = window.pageXOffset;
y = window.pageYOffset;
} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
// DOM
x = document.body.scrollLeft;
y = document.body.scrollTop;
} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
// IE6 standards compliant mode
x = document.documentElement.scrollLeft;
y = document.documentElement.scrollTop;
}
return [x, y];
}
function setScrollXY(x, y) {
window.scrollTo(x, y);
}
The getscrollXY function doesn’t seem to work in IE7.0,
the values returned is always [0,0]
Make sure the scrollbar is not actually at the [0,0] position when you call the function. I just tested it in IE7 version 7.0.5730.11 and it outputs the correct results.
It depends on the DOCTYPE in IE7. I had to use documentElement for IE7 because of my doctype. Because body.scrollLeft and body.scrollTop still have values and come out as true using your code. But the values are 0,0 as Anthony stated.