今天早上有个帖子问有多少人禁用JavaScript。然后我开始想知道可以使用什么技术来确定用户是否禁用了它。

有人知道一些简单的方法来检测JavaScript是否被禁用吗?我的意图是给一个警告,如果浏览器没有启用JS,站点将无法正常运行。

最终,我想把它们重定向到能够在没有JS的情况下工作的内容,但我需要这个检测作为一个占位符来开始。


当前回答

也许听起来很奇怪,但你可以试一试:

<?php $jsEnabledVar = 0; ?>    

<script type="text/javascript">
var jsenabled = 1;
if(jsenabled == 1)
{
   <?php $jsEnabledVar = 1; ?>
}
</script>

<noscript>
var jsenabled = 0;
if(jsenabled == 0)
{
   <?php $jsEnabledVar = 0; ?>
}
</noscript>

现在在整个页面中使用'$jsEnabledVar'的值。你也可以用它来显示一个块,告诉用户JS被关闭了。

希望这能有所帮助

其他回答

Why don't you just put a hijacked onClick() event handler that will fire only when JS is enabled, and use this to append a parameter (js=true) to the clicked/selected URL (you could also detect a drop down list and change the value- of add a hidden form field). So now when the server sees this parameter (js=true) it knows that JS is enabled and then do your fancy logic server-side. The down side to this is that the first time a users comes to your site, bookmark, URL, search engine generated URL- you will need to detect that this is a new user so don't look for the NVP appended into the URL, and the server would have to wait for the next click to determine the user is JS enabled/disabled. Also, another downside is that the URL will end up on the browser URL and if this user then bookmarks this URL it will have the js=true NVP, even if the user does not have JS enabled, though on the next click the server would be wise to knowing whether the user still had JS enabled or not. Sigh.. this is fun...

我假设您正在决定是否交付javascript增强的内容。最好的实现能够干净地降级,因此站点在没有JavaScript的情况下仍然可以运行。我还假定您指的是服务器端检测,而不是出于某种无法解释的原因使用<noscript>元素。

没有执行服务器端JavaScript检测的好方法。作为一种替代方法,可以使用JavaScript设置一个cookie,然后在后续的页面视图中使用服务器端脚本测试该cookie。然而,这并不适合决定要传递什么内容,因为它无法区分没有cookie的访问者与新访问者或不接受JavaScript集cookie的访问者。

为什么需要知道服务器端是否启用了JavaScript ?浏览器支持哪种变体有关系吗?它是否需要理解关键字let或者只是var ?

我建议发送可读的内容,不需要任何JavaScript可访问,然后只是尝试加载JS文件添加所有你想要的JS行为。

For example, the UI might end up missing Login or Modify button if JS is not enabled and it might include a small text at the bottom (using <noscript> or some element with CSS animation that shows the text after a small delay if JS code doesn't remove the whole element soon enough) saying "To login/modify this content, you must enable JavaScript support in your browser." If you do this well, the reader may not even notice that anything is missing unless he or she is trying to login or modify the content.

作为一种优化,你可以用JavaScript设置cookie,如果你想异步获取它,服务器可以避免发送非JavaScript可读的内容。只是要确保在JS代码运行完成至少一次之后才设置这个cookie,而不是在JS代码开始运行时立即设置它,以确保当JS代码由于任何原因失败时(不是如果!)最终用户不会以空白屏幕结束。

(请注意,异步加载初始页面状态不会更快地将内容传递给最终用户。但是,如果没有JavaScript,您只能发送全部内容的一部分。这可以更快地呈现“优先”,然后使用JS代码异步加载页面的其余部分。)

作为一个额外的好处,搜索引擎仍然可以在不启用任何JavaScript的情况下索引您的站点。

noscript标记工作得很好,但是需要每个额外的页面请求来继续提供无用的JS文件,因为noscript本质上是一个客户端检查。

你可以用JS设置一个cookie,但正如其他人指出的那样,这可能会失败。理想情况下,您希望能够检测到JS客户端,并且不使用cookie,为该用户设置会话服务器端,以指示是否启用JS。

A possibility is to dynamically add a 1x1 image using JavaScript where the src attribute is actually a server side script. All this script does is saves to the current user session that JS is enabled ($_SESSION['js_enabled']). You can then output a 1x1 blank image back to the browser. The script won't run for users who have JS disabled, and hence the $_SESSION['js_enabled'] won't be set. Then for further pages served to this user, you can decide whether to include all of your external JS files, but you'll always want to include the check, since some of your users might be using the NoScript Firefox add-on or have JS disabled temporarily for some other reason.

您可能希望将此检查包含在接近页面末尾的位置,以便额外的HTTP请求不会减慢页面的呈现速度。

对于那些只想跟踪js是否被启用的人,如何使用ajax例程来存储状态?例如,我将所有访问者/访问记录在一组表中。JSenabled字段可以设置为默认值FALSE,如果启用了JS, ajax例程会将其设置为TRUE。