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?
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?
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.
$(document.body)[0].scrollHeight
instead of it you can also use $(document).height()
$(selector)[0].scrollHeight
This selector is not supported by lesser than IE version 8.0