0

To check if the page has a scrollbar I used:

<script>
if (document.body.scrollWidth > document.body.offsetWidth){alert('exist');}
</script>

But it doesn't work.

How can I check if scrollbar exists on page?

PaulG
  • 13,871
  • 9
  • 56
  • 78

1 Answers1

2

You can check the scrollHeight of the document:

if($(document.body)[0].scrollHeight > $(window).height()){
    alert('vertical scroll exists.');
}

A simple test scenario:

css:

body{height:2000px;} // here body tag of the document is 2000px in height

so the js would be like this:

if($('body')[0].scrollHeight > $(window).height()){
    alert('vertical scroll exists.');
}

or

if($(document.body)[0].scrollHeight > $(window).height()){
    alert('vertical scroll exists.');
}

here $('body')[0].scrollHeight you need to select your document's body.

Test Fiddle.


$(document.body)[0].scrollHeight instead of it you can also use $(document).height()

Note:

$(selector)[0].scrollHeight This selector is not supported by lesser than IE version 8.0

Community
  • 1
  • 1
Jai
  • 74,255
  • 12
  • 74
  • 103