Detect window scrollbars

Clean JavaScript to detect if the window has a scrollbars.

JavaScript code

function getDocumentHeight() {
    return Math.max(
		document.body.scrollHeight,
		document.body.offsetHeight,
		document.documentElement.clientHeight,
		document.documentElement.scrollHeight,
		document.documentElement.offsetHeight
    );
}
function getDocumentWidth() {
    return Math.max (
		document.body.scrollWidth,
		document.body.offsetWidth,
		document.documentElement.clientWidth,
		document.documentElement.scrollWidth,
		document.documentElement.offsetWidth
    );
}
function windowHasScrollBarY () {
    return (getDocumentHeight() > window.innerHeight);
}


function widnowHasScrollBarX () {
    return (getDocumentWidth() > window.innerWidth);
}
if ( windowHasScrollBarY() ) {
    // There is a vertical scrollbar
}
if (widnowHasScrollBarX() ) {
    // There is a horizontal scrollbar
}

Demo

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.