When I put this Doctype in my documents document.body.scrollTop
returns zero.
When you use that Doctype, you'll put every current browser in the so-called Almost Standards mode, without it you'll be in Quirks Mode.
As you can read on this page,
[m]ost browsers provide
window.pageXOffset/pageYOffset
. These are completely reliable. Once again, Internet Explorer is the odd one out, as it does not provide these properties. Internet Explorer and some other browsers will providedocument.body.scrollLeft/Top
. In strict mode, IE 6 and a few other browsers, providedocument.documentElement.scrollLeft/Top
.
A script provided there calculates the value you want:
function getScrollXY() {
var scrOfX = 0, scrOfY = 0;
if( typeof( window.pageYOffset ) == 'number' ) {
//Netscape compliant
scrOfY = window.pageYOffset;
scrOfX = window.pageXOffset;
} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
//DOM compliant
scrOfY = document.body.scrollTop;
scrOfX = document.body.scrollLeft;
} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
//IE6 standards compliant mode
scrOfY = document.documentElement.scrollTop;
scrOfX = document.documentElement.scrollLeft;
}
return [ scrOfX, scrOfY ];
}
Another interesting article appeared on QuirksMode, A tale of two viewports.