Hide Scrollbars Using Javascript
Posted by tech on
November 20, 2008
One issue I came across while doing one of my projects was to hide the scrollbars of a popup window. Yeah, sure, it is easy if you add the attribute scrollbars=no inside the window.open()‘s window attributes. But what if you wish to hide them when the popup window is already open. Thank you CSS! Yes, it is possible by using Javascript and accessing the window style’s property overflow. However, there is a cross-browser issue. Both IE and Firefox/Google Chrome have different codes that make this work. You can place both their codes in the same page and/or Javascript function. If the browser does not understand the property, it will ignore it anyway so there is no need to do a browser check to execute the code specific to that certain browser.
The code below does the trick
1 2 | document.documentElement.style.overflow = 'hidden'; // firefox, chrome document.body.scroll = "no"; // ie only |
Just in case you wish to disable either just the horizontal or vertical scrollbar, this code works for non IE browsers
1 2 | document.documentElement.style.overflowX = 'hidden'; // horizontal scrollbar will be hidden document.documentElement.style.overflowY = 'hidden'; // vertical scrollbar will be hidden |











January 29th, 2009 at 8:00 pm
Hi,
my name is Malte Newman, I am a developer. I had a 3 days issue and your code solved the problem. I just want to say “thank you” for providing the code. How could you find out this?
I am loading several CSS backgrounds with an asp file in, and no matter I tried, I had always a horizontal scrollbar and a white empty space on the right side of the screen. So I tried to eliminate the hor scrollbar with code like:
>window.document.body.style.overflow = ‘hidden’; and I added to all >divs< the ‘overflow’ but none of them worked - 3 days.
So I just wonna say “thank you”.
Best regards,
Malte Newman
January 29th, 2009 at 8:09 pm
your code has a window. in it, are you doing that to a popup window? because if not, then that should not work because of the window.
December 19th, 2009 at 1:56 am
Thanks a lot dude for such a useful tip//
March 13th, 2010 at 4:54 pm
Very useful, thanks. I set it up so that it hides the scrollbar until everything is loaded and then is shows the scrollbar.
It worked great in Firefox and IE and does indeed hide the scrollbar in Safari, but doesn’t bring it back once everything is loaded in Safari. Any ideas? I’m using the $(window)load function to bring it back once everything is loaded… Thanks!
March 13th, 2010 at 6:37 pm
diss safari man … it sucks lol