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

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

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


当前回答

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请求不会减慢页面的呈现速度。

其他回答

我认为你可以在noscript标签中插入一个图像标签,并查看你的站点有多少次以及这个图像被加载的频率。

更新2022

这个问题已经有30多个答案,但似乎没有一个答案清楚或准确地说明必须做什么。

服务器端框架使用- ASP。网络核心 客户端-香草js

在BaseController上,也就是应用的第一个入口点OnActionExecutionAsync上,我有这个逻辑。

基本上默认情况下,我假设客户端没有启用javascript并设置此标志。

Response.Cookies.Append("jsEnabled", "false");

现在,在客户端初始加载后,我有一个javascript函数,将这个标志更新为true。 这个函数只有在环境中有javascript时才会运行

完整的解在这里。

客户端

在初始负载上添加此函数

function detectIfJavascriptIsEnabled() {
    // if this function run's which means js is enabled
    var jsEnabled = getCookie('jsEnabled');
    if (jsEnabled === 'false') {
        setCookie('jsEnabled', 'true');
        location.reload();
    }
}

服务器

private bool ValidateIfEnvironmentHasJavascript() {
  if (HttpContext.Request.Cookies != null && HttpContext.Request.Cookies.Count > 0) {
    Boolean.TryParse(HttpContext.Request.Cookies["jsEnabled"], out
      var hasJavascriptEnabled);
    return hasJavascriptEnabled;
  } else {
    Response.Cookies.Append("jsEnabled", "false",
      new CookieOptions() {
        IsEssential = true, Expires = DateTime.UtcNow.AddHours(24)
      });
  }

  return false;
}

这就是得到结果的方法

var environmentHasJavascript = ValidateIfEnvironmentHasJavascript();

我建议您反过来编写不引人注目的JavaScript。

使您的项目的功能为禁用JavaScript的用户工作,当您完成后,实现您的JavaScript ui增强。

https://en.wikipedia.org/wiki/Unobtrusive_JavaScript

因为我总是想给浏览器一些有价值的东西,我经常使用这个技巧:

首先,页面中任何需要JavaScript才能正常运行的部分(包括通过getElementById调用等修改的被动HTML元素)都被设计成可用的,假设没有可用的JavaScript。(设计得好像它不存在一样)

任何需要JavaScript的元素,我在标签中放置如下内容:

<span name="jsOnly" style="display: none;"></span>

然后在文档的开头,使用.onload或document。在getElementsByName('jsOnly')的循环中设置.style。Display = "";重新打开JS依赖的元素。这样,非JS的浏览器就不需要看到站点中依赖于JS的部分,如果他们拥有它,它会在准备就绪时立即显示出来。

一旦您习惯了这种方法,就很容易混合您的代码来处理这两种情况,尽管我现在只是在试验noscript标记,并期望它会有一些额外的优点。

您不会检测用户是否禁用了javascript(服务器端或客户端)。相反,你假设javascript是禁用的,并建立你的网页与javascript禁用。这消除了对noscript的需要,无论如何都应该避免使用noscript,因为它不能很好地工作,而且是不必要的。

例如,只要建立你的网站说<div id="nojs">这个网站没有JS就不能工作

然后,脚本将简单地执行document.getElementById('nojs').style。Display = 'none';并继续其正常的JS业务。