Update “overflow: hidden” style of the HTML tag error in IE

March 16th, 2010 by paul Leave a reply »

When trying to update the “overflow: hidden” style attribute of either the body or the html tag using JavaScript, unsurprisingly, IE throws an error… However using the magic of prototype I found a very simple way of achieving this that keeps Microsoft happy – even IE6.

So the two main javascript calls I was trying were:

document.body.style.overflow = 'hidden';
document.body.setStyle({'overflow': 'hidden'});

(The latter using prototype‘s handy setStyle() method) both of which failed. In the end the very simple solution (which I’m posting because I didn’t find written anywhere else) was adding the following css to my global stylesheet

html.overflow-hidden body {
    overflow: hidden;
}

add the following to an IE specific stylesheet

html.overflow-hidden {
    overflow: hidden;
}

that should be included in the section of the page using

<!--[if lte IE 7]>
<style type="text/css" media="screen">
@import url("/css/ie7.css");
</style>
<![endif]-->

and then using the following javascript to toggle the overflow property:

$$('html').invoke('addClassName', 'overflow-hidden');

and

$$('html').invoke('removeClassName', 'overflow-hidden');

So like I said, super simple, but works great!

Google+
Advertisement

4 comments

  1. MarkSpizer says:

    great post as usual!

  2. Thanks says:

    Did the trick (IE8) as described.

  3. Dave Schafer says:

    This worked great! Thanks for the contribution. I spot tested this in FF5, Chrome12, IE8, and IE9 (more to go).

    By the way, though I expected that the IE workaround described above would be necessary for IE8, it was indeed necessary for IE9 as well. IE9 is a much-needed improvement on IE8 but IE9 did not work without your IE workaround. So, at this time, I would simply use [if IE]… for this solution.

    Thanks again!

Trackbacks /
Pingbacks

  1. promotional gifts

Leave a Reply