What is the best cross-browser way to detect the scrollTop of the browser window? I prefer not to use any pre-built code libraries because this is a very simple script, and I don't need all of that deadweight.
11 Answers
function getScrollTop(){
if(typeof pageYOffset!= 'undefined'){
//most browsers except IE before #9
return pageYOffset;
}
else{
var B= document.body; //IE 'quirks'
var D= document.documentElement; //IE with doctype
D= (D.clientHeight)? D: B;
return D.scrollTop;
}
}
alert(getScrollTop())

- 102,654
- 32
- 106
- 127
-
4This method doesn't seem to work with Firefox browsers on Mac or Linux. – hypervisor666 Jul 30 '13 at 20:25
-
1ugly alert! use console and console.log instead! , anyway good answer, but what are the browser support of the "//most browsers" - or better IEwhatnumber+ is supporting that ? – jave.web Aug 10 '13 at 08:13
-
1is this short version correct enough? `var scrollTop = pageYOffset || (document.documentElement.clientHeight ? document.documentElement.scrollTop : document.body.scrollTop);` – Roman Dec 30 '14 at 10:44
-
1@Roman I think it would only fail if pageYOffset were 0 which would probably mean the other part would be undefined judging by other comments on this page, so if you just add an or 0 at the end like so it should work. Like so, `var scrollTop = pageYOffset || (document.documentElement.clientHeight ? document.documentElement.scrollTop : document.body.scrollTop) || 0;` – John Jul 10 '18 at 04:21
-
1Use window.pageYOffset instead of just pageYOffset for Brave Browser to avoid intermittent undefined errors when called before window is available. – Loren Apr 22 '20 at 18:10
Or just simple as:
var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;

- 3,554
- 3
- 25
- 41

- 427
- 5
- 7
-
6Wouldn't work on iPad and possibly other devices/browsers (Chrome?), you will need `window.pageYOffset` there. – Yeti Oct 08 '13 at 11:07
-
3
-
Works for me in *mobile* Safari 5 and 7, FireFox 31, Chrome 37, Opera 24, IE 5-11. According to http://www.quirksmode.org/dom/w3c_cssom.html#t35 this is *so* cross-browser. So thank you! Short'n'easy. – flu Sep 29 '14 at 13:00
-
This is the most useful answer here, without even needing a function (in terms of style). – fjaguero Sep 03 '15 at 07:48
If you don't want to include a whole JavaScript library, you can often extract the bits you want from one.
For example, this is essentially how jQuery implements a cross-browser scroll(Top|Left):
function getScroll(method, element) {
// The passed in `method` value should be 'Top' or 'Left'
method = 'scroll' + method;
return (element == window || element == document) ? (
self[(method == 'scrollTop') ? 'pageYOffset' : 'pageXOffset'] ||
(browserSupportsBoxModel && document.documentElement[method]) ||
document.body[method]
) : element[method];
}
getScroll('Top', element);
getScroll('Left', element);
Note: you'll notice that the above code contains a browserSupportsBoxModel
variable which is undefined. jQuery defines this by temporarily adding a div to the page and then measuring some attributes in order to determine whether the browser correctly implements the box model. As you can imagine this flag checks for IE. Specifically, it checks for IE 6 or 7 in quirks mode. Since the detection is rather complex, I've left it as an exercise for you ;-), assuming you have already used browser feature detection elsewhere in your code.
Edit: If you haven't guessed already, I strongly suggest you use a library for these sorts of things. The overhead is a small price to pay for robust and future-proof code and anyone would be much more productive with a cross-browser framework to build upon. (As opposed to spending countless hours banging your head against IE).

- 10,735
- 1
- 31
- 44
I've been using window.scrollY || document.documentElement.scrollTop
.
window.scrollY
covers all browsers except IEs.
document.documentElement.scrollTop
covers IE.

- 7,074
- 1
- 35
- 43
-
3The first expression can also be `window.pageYOffset`. I mention this because `pageYOffset` seems to be more historical, and I don’t see `scrollY` quite so often. According to MDN, they’re the same. In any case, this is by far the cleanest and most direct solution, and should be preferred because it addresses modern browsers first, thus not punishing them for being up-to-date. – Manngo Feb 04 '17 at 02:36
-
1@Jason: I meant to add that I was talking about your answer to the question being the cleanest & most direct, rather than my own comment above. – Manngo Feb 04 '17 at 05:46
function getSize(method) {
return document.documentElement[method] || document.body[method];
}
getSize("scrollTop");

- 3,542
- 1
- 26
- 23
YUI 2.8.1 code does this:
function getDocumentScrollTop(doc)
{
doc = doc || document;
//doc.body.scrollTop is IE quirkmode only
return Math.max(doc.documentElement.scrollTop, doc.body.scrollTop);
}
I think jQuery 1.4.2 code (a bit translated for humans) and supposing I understood it properly does this:
function getDocumentScrollTop(doc)
{
doc = doc || document;
win = doc.defaultView || doc.parentWindow; //parentWindow is for IE < 9
result = 0;
if("pageYOffset" in win) //I'don't know why they use this, probably they tested it to be faster than doing: if(typeof win.pageYOffset !== 'undefined')
result = win.pageYOffset;
else
result = (jQuery.support.boxModel && document.documentElement.scrollTop) ||
document.body.scrollTop;
return result;
}
Unfortunatley extracting the value of jQuery.support.boxModel
is almost impossible because you would have to add a temporary child element into document and do the same tests jQuery does.

- 33,578
- 33
- 128
- 159
I know its been quite a while since this thread was updated, but this is a function I created that allows developers to find the root element that actually hosts has a working "scrollTop" property. Tested on Chrome 42 and Firefox 37 for Mac OS X (10.9.5):
function getScrollRoot(){
var html = document.documentElement, body = document.body,
cacheTop = ((typeof window.pageYOffset !== "undefined") ? window.pageYOffset : null) || body.scrollTop || html.scrollTop, // cache the window's current scroll position
root;
html.scrollTop = body.scrollTop = cacheTop + (cacheTop > 0) ? -1 : 1;
// find root by checking which scrollTop has a value larger than the cache.
root = (html.scrollTop !== cacheTop) ? html : body;
root.scrollTop = cacheTop; // restore the window's scroll position to cached value
return root; // return the scrolling root element
}
// USAGE: when page is ready: create a global variable that calls this function.
var scrollRoot = getScrollRoot();
scrollRoot.scrollTop = 10; // set the scroll position to 10 pixels from the top
scrollRoot.scrollTop = 0; // set the scroll position to the top of the window
I hope you find this useful! Cheers.

- 21
- 2
This works well from IE5 to IE11. It also supports all major new browsers.
var scrollTop = (document.documentElement && document.documentElement.scrollTop) ||
(document.body.scrollTop) || (document.scrollingElement);

- 311
- 2
- 11
- 26
-
When the scroll position is 0, this will return `` when logging this to the console. Most likely this is happening due to this being a one-liner and a document.body.scrollTop of 0 equals false, thus returning the scrollingElement. – Harti Jul 21 '18 at 07:31
-
Should the last item be `document.scrollingElement.scrollTop` instead of `document.scrollingElement`? – blackr1234 Sep 18 '20 at 04:20
No need extra library, use this snippet:
const scrollTop =
(window.pageYOffset !== undefined) ?
window.pageYOffset :
(document.documentElement || document.body.parentNode || document.body).scrollTop;
I believe the best way to get scrollTop in modern browsers is to use
const scrollTop = document.scrollingElement.scrollTop
if this doesn't work you could also try
const scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop, document.scrollingElement.scrollTop)

- 1
to save you all the trouble use a framework such as jquery or mootools that calculates all these values in one line (cross browser) in mootools its $('element').getTop(); in jquery you will need a plugin named dimensions if i remember correctly although most of the time even without a framework you can actually use element.getScrollTop(); to get what you'll need the only problem is on IE7 and below this calculation is somewhat buggy as it doesn not take the position value of the element into consideration for example if you got a position: absolute css attribute for that element the calculation is performed on the parent element of that element

- 6,404
- 18
- 58
- 76
-
FYI - The question states "I prefer not to use any pre-built code libraries". You got marked down as mooTools is still a pre-built code library. Thanks for taking the time to contribute, just please read the question a bit more thoroughly or your rep will take a hit – iancrowther May 08 '13 at 11:52
-
Also, another problem with this approach that if you are trying to detect it for the main window scrollbar, non-IE browsers will scroll the element and IE will scroll . So, you have to figure out what 'element' is first. – Neil Monroe Sep 27 '13 at 00:58