Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
157 views
in Technique[技术] by (71.8m points)

javascript - 如何检测是否禁用了JavaScript?(How to detect if JavaScript is disabled?)

There was a post this morning asking about how many people disable JavaScript.

(今天早上有一篇帖子问有多少人禁用JavaScript。)

Then I began to wonder what techniques might be used to determine if the user has it disabled.

(然后我开始想知道可以使用什么技术来确定用户是否禁用了它。)

Does anyone know of some short/simple ways to detect if JavaScript is disabled?

(有谁知道一些简单/简单的方法来检测JavaScript是否被禁用?)

My intention is to give a warning that the site is not able to function properly without the browser having JS enabled.

(我的目的是警告您,如果没有启用JS的浏览器,站点将无法正常运行。)

Eventually I would want to redirect them to content that is able to work in the absence of JS, but I need this detection as a placeholder to start.

(最终,我想将它们重定向到可以在没有JS的情况下运行的内容,但是我需要将此检测作为占位符才能启动。)

  ask by community wiki translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'd like to add my .02 here.

(我想在这里添加我的.02。)

It's not 100% bulletproof, but I think it's good enough.

(它不是100%防弹的,但我认为它足够好。)

The problem, for me, with the preferred example of putting up some sort of "this site doesn't work so well without Javascript" message is that you then need to make sure that your site works okay without Javascript.

(对于我来说,问题是出现一些“没有Java脚本,该站点无法正常运行”消息的首选示例,那么您需要确保您的站点在没有Java脚本的情况下可以正常工作。)

And once you've started down that road, then you start realizing that the site should be bulletproof with JS turned off, and that's a whole big chunk of additional work.

(一旦走上了这条路,您就开始意识到该站点应该在关闭JS的情况下是防弹的,这是一大堆额外的工作。)

So, what you really want is a "redirection" to a page that says "turn on JS, silly".

(因此,您真正想要的是“重定向”到一个页面,该页面显示“傻傻地打开JS”。)

But, of course, you can't reliably do meta redirections.

(但是,当然,您不能可靠地进行元重定向。)

So, here's the suggestion:

(所以,这是建议:)

<noscript>
    <style type="text/css">
        .pagecontainer {display:none;}
    </style>
    <div class="noscriptmsg">
    You don't have javascript enabled.  Good luck with that.
    </div>
</noscript>

...where all of the content in your site is wrapped with a div of class "pagecontainer".

(...您网站中的所有内容都被一个“ pagecontainer”类的div包裹。)

The CSS inside the noscript tag will then hide all of your page content, and instead display whatever "no JS" message you want to show.

(然后,noscript标记内的CSS将隐藏您的所有页面内容,而是显示要显示的任何“ no JS”消息。)

This is actually what Gmail appears to do...and if it's good enough for Google, it's good enough for my little site.

(这实际上就是Gmail的功能……如果对Google足够好,对我的小网站也足够好。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...