admin管理员组

文章数量:1325409

If no JavaScript is enabled, I want my body to have overflow:hidden (no scrollbars).

How can I solve this? Is it possible to use a noscript tag inside of <head> and set specific styles inside of it?

If no JavaScript is enabled, I want my body to have overflow:hidden (no scrollbars).

How can I solve this? Is it possible to use a noscript tag inside of <head> and set specific styles inside of it?

Share Improve this question edited Oct 2, 2011 at 18:17 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Nov 23, 2010 at 9:53 mattmatt 44.4k107 gold badges268 silver badges402 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

If you mean the <body> of the HTML document, I'd build the HTML as it should be for the users with JS disabled. For example in the CSS file have body { overflow:hidden } and then use JavaScript to set the page as it should be for users with JavaScript enabled (i.e. remove the overflow:hidden with JavaScript).

I'm pretty sure

<noscript>
    <style type="text/css">
        body {
            overflow:hidden;
        }
    </style>
</noscript>

in your <head> would do the job.

But as stated in the other post, it may be better to use CSS for this purpose, and enable overflow for browsers with JavaScript.

It's not valid using the <noscript> tag inside the head section (it is allowed only inside <body>), so you could use this workaround instead (an HTML5 example)

 <!doctype html>
 <html lang="en">
     <head>
         <script>document.documentElement.className = 'js';</script>
         <style>
             body { overflow: hidden; }
             html.js body { overflow: auto; }
         </style>
     </head>

This is probably a super overkill, but just for general knowledge:

Modernizr uses a mechanism where it suggest you put the no-js class on your html tag. If modernizr runs, it removes the no-js class. Using modernizr would be an overkill, but you could do this yourself. In your css you would have:

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

本文标签: javascriptBody styling for a noscript tagStack Overflow