Body styling for a noscript tag?

自闭症网瘾萝莉.ら 提交于 2019-12-24 04:25:20

问题


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?


回答1:


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.




回答2:


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).




回答3:


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>



回答4:


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;
}


来源:https://stackoverflow.com/questions/4254729/body-styling-for-a-noscript-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!