Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6129611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:45:47+00:00 2026-05-23T16:45:47+00:00

I need to be able to detect whether there are scrollbars (both vertical and

  • 0

I need to be able to detect whether there are scrollbars (both vertical and horizontal) on a browser window. I’ve been using this code but it isn’t working reliably in Firefox 5.

JFL.GetScrollbarState = function () {
    var myWidth = 0;
    var myHeight = 0;
    if (document.documentElement && document.documentElement.clientWidth) {
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else {
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    return ({
        vScrollbar: document.body.scrollHeight > myHeight,
        hScrollbar: document.body.scrollWidth > myWidth
    });
}

Is there a better way to do this that will work cross browser. My browser targets are Firefox 4-5, Chrome, Safari 4+, Opera 10+.

If you’re interested in why I need to know if there are scrollbars, it’s because I have some spinning CSS3 transitions that (due to the nature of their spinning) may temporarily go beyond the edges of the current document size (thus making the document temporarily larger). If were no scrollbars initially present, the CSS3 transition may cause scrollbars to show up during the transition and then go away when the transition is finished, leading to an ugly scrollbar flash. If I know that there are no scrollbars present, I can temporarily add a class that will set overflow-x or overflow-y to hidden and thus prevent the scrollbar flash during the CSS3 transition. If scrollbars are already present, I don’t have to do anything because they may move a little, but they won’t go on/off during the transition.

Bonus points if one can actually tell not only if scrollbars would generally be required, but whether they are actually there or not.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:45:47+00:00Added an answer on May 23, 2026 at 4:45 pm

    After running into flicker problems with the scrolling version proposed by David in some browsers (Safari and IE), I’ve settled on this code that does not have the flicker problem:

    function getScrollBarState() {
        var result = {vScrollbar: true, hScrollbar: true};
        try {
            var root = document.compatMode=='BackCompat'? document.body : document.documentElement;
            result.vScrollbar = root.scrollHeight > root.clientHeight;
            result.hScrollbar = root.scrollWidth > root.clientWidth;
        } catch(e) {}
        return(result);
    }
    

    It’s a derivative of what I was using and the general technique was referenced in the post that fanfavorite posted. It seems to work in every browser I’ve tried even in IE6. For my purposes, I wanted any failure to return that there was a scrollbar so I’ve coded the failure condition that way.

    Note: this code does not detect if a scrollbar has been forced on or off with CSS. This code detects if an auto-scrollbar is called for or not. If your page might have CSS settings that control the scrollbar, then you can get the CSS and check that first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to be able to detect whether the current language my user is
I need to be able to detect whether the current language my user is
In my engine I have a need to be able to detect DXT1 textures
I have a TileList in flex, and need to be able to detect if
On the client machine I need to be able to somehow detect which sites
I need to be able to detect touch events on the keyboard. I have
I need to be able to detect when an icon is dropped outside its
I'm writing a qt-based c++ application and i need to be able to detect
I have a web application in C#. I need to be able to detect
I need to be able to detect which version of Excel I have installed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.