6

I need to know, whether a vertical scrollbar has appeared or not in browser window. Is it possible using jQuery or any other way?

orion3
  • 9,797
  • 14
  • 67
  • 93

3 Answers3

7

Like this:

if (document.documentElement.scrollHeight === document.documentElement.clientHeight) {
    //There is no vertical scrollbar
}

This doesn't work in IE

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • The scroll-bar may be explicitly hidden via `body { overflow-y: hidden; }`, in which case your code would report a false positive... – Šime Vidas Mar 28 '12 at 19:20
  • This also doesn't work for when the body itself is small, but an absolutely positioned element has moved partially beyond the viewport and caused the UA to show scrollbars (the body height/width doesn't change in that instance, but there are definitely scrollbars present). – Robert C. Barth Jul 18 '12 at 22:57
4

This is an old post but I finally got the code that also works on IE7. Hope this can help someone.

var hasScrollbar = $('body').outerHeight() > $(window).height();
Aximili
  • 28,626
  • 56
  • 157
  • 216
  • This doesn't work for absolutely positioned elements that are beyond the viewport (causing scrollbars) when the body itself is small. – Robert C. Barth Jul 18 '12 at 22:55
1

Compare the document height with the window height. If it's more there's probably a scrollbar unless you disabled it.

CharlesLeaf
  • 3,201
  • 19
  • 16