如何检查浏览器的 JavaScript 是否启用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3739100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check browser's JavaScript is enabled or not
提问by Simhadri
My application depends on JavaScript, I want to check the client browser's JavaScript is enabled or not and raise an alert message if its turned off.
我的应用程序依赖于 JavaScript,我想检查客户端浏览器的 JavaScript 是否启用,并在它关闭时发出警报消息。
回答by Franci Penov
Put the message in a <div>that's wrapped in a <noscript>tag. If JavaScript is disabled, the <div>will be rendered as part of the DOM; if script is enabled, the div won't be in the DOM.
把消息中的<div>多数民众赞成包裹在一个<noscript>标签。如果 JavaScript 被禁用,<div>将作为 DOM 的一部分呈现;如果启用脚本,则 div 将不在 DOM 中。
For example, you can put the following immediately after the opening <body>tag, and style it through CSS to have red background to make it more prominent.
例如,您可以在开始<body>标记后立即放置以下内容,并通过 CSS 将其设置为红色背景,使其更加突出。
<noscript>
<div id="js-warning">
To be able to access all of our features, you need a browser that supports JavaScript, and it needs to be enabled.
</div>
</noscript>
回答by Nathan Wheeler
There's actually a <noscript>tag that you can use to display the content contained inside when javascript is not available.
<noscript>当 javascript 不可用时,实际上有一个标签可以用来显示其中包含的内容。
Something like:
就像是:
<noscript>
<div>
You must enable javascript to continue.
</div>
</noscript>
The div just won't show if they have javascript, and it's pretty easy to tell if javascript IS working, no matter whether you need it to ping your server back to let it know, or use it to perform some more advanced functions.
div 不会显示它们是否有 javascript,而且很容易判断 javascript 是否正在工作,无论您是否需要它来 ping 您的服务器以使其知道,或者使用它来执行一些更高级的功能。
回答by foo
"Raise an alert message if its turned off" is paradox, since if it is turned off, you cannot "do" anything programatically.
“如果关闭则发出警报消息”是矛盾的,因为如果它被关闭,你就不能以编程方式“做”任何事情。
You can do it the other way 'round, however: make that message appear by default, and have JavaScript hide it if it is turned on (e.g. by setting a DIVs visibility to hidden),
但是,您可以以相反的方式执行此操作:默认情况下显示该消息,并在打开时让 JavaScript 隐藏它(例如,通过将 DIV 可见性设置为隐藏),
or you rely on the standard compliance of the browser and use the <noscript> tag. Stuff inside the <noscript> gets shown if no javascript is enabled. BTW, make sure to set the type="text/javascript" attribute of the script tag.
或者您依赖浏览器的标准合规性并使用 <noscript> 标签。如果未启用 javascript,则 <noscript> 中的内容会显示。顺便说一句,请确保设置脚本标记的 type="text/javascript" 属性。
See also http://www.w3.org/TR/REC-html40/interact/scripts.html#h-18.3
另见http://www.w3.org/TR/REC-html40/interact/scripts.html#h-18.3
回答by IrishChieftain
A browser may be "JavaScript capable" but that does not mean that JavaScript has not been disabled by the user or by an admin. There is no real way to determine this. Best practices dictate "Progressive Enhancement"; that is, you application needs to work without JavaScript first - then add the JavaScript functionality for those (most) that have it enabled.
浏览器可能“支持 JavaScript”,但这并不意味着用户或管理员没有禁用 JavaScript。没有真正的方法来确定这一点。最佳实践决定了“渐进增强”;也就是说,您的应用程序需要首先在没有 JavaScript 的情况下工作 - 然后为那些(大多数)启用它的人添加 JavaScript 功能。
http://www.alistapart.com/articles/progressiveenhancementwithjavascript/
http://www.alistapart.com/articles/progressiveenhancementwithjavascript/
http://www.webcredible.co.uk/user-friendly-resources/dom-scripting/progressive-enhancement.shtml
http://www.webcredible.co.uk/user-friendly-resources/dom-scripting/progressive-enhancement.shtml
Avoid hacky solutions to this and bear in mind that there are also accessibility issues for people with screen readers. <noscript> content only displays if JavaScript is disabled. Most screen reader users have JavaScript enabled, so they will see your inaccessible script rather than the <noscript> content.
避免对此使用hacky解决方案,并记住对于使用屏幕阅读器的人来说也存在可访问性问题。<noscript> 内容仅在禁用 JavaScript 时显示。大多数屏幕阅读器用户都启用了 JavaScript,因此他们将看到您无法访问的脚本而不是 <noscript> 内容。

