今天早上有个帖子问有多少人禁用JavaScript。然后我开始想知道可以使用什么技术来确定用户是否禁用了它。
有人知道一些简单的方法来检测JavaScript是否被禁用吗?我的意图是给一个警告,如果浏览器没有启用JS,站点将无法正常运行。
最终,我想把它们重定向到能够在没有JS的情况下工作的内容,但我需要这个检测作为一个占位符来开始。
今天早上有个帖子问有多少人禁用JavaScript。然后我开始想知道可以使用什么技术来确定用户是否禁用了它。
有人知道一些简单的方法来检测JavaScript是否被禁用吗?我的意图是给一个警告,如果浏览器没有启用JS,站点将无法正常运行。
最终,我想把它们重定向到能够在没有JS的情况下工作的内容,但我需要这个检测作为一个占位符来开始。
当前回答
更新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();
其他回答
你可以使用一个简单的JS代码片段来设置隐藏字段的值。当发布回来时,你知道JS是否被启用。
或者你可以尝试打开一个弹出窗口,然后迅速关闭(但可能是可见的)。
你也有NOSCRIPT标签,你可以用它在禁用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...
noscript块在JavaScript被禁用时执行,通常用于显示你用JavaScript生成的替代内容,例如:
<script type="javascript">
... construction of ajaxy-link, setting of "js-enabled" cookie flag, etc..
</script>
<noscript>
<a href="next_page.php?nojs=1">Next Page</a>
</noscript>
没有js的用户会得到next_page链接——你可以在这里添加参数,这样你就可以在下一页知道他们是否通过js /非js链接来访问,或者试图通过js设置cookie,如果没有这些就意味着js被禁用了。这两个例子都非常琐碎,可以进行操作,但是您可以理解其中的含义。
如果你想知道有多少用户禁用了javascript,你可以这样做:
<noscript>
<img src="no_js.gif" alt="Javascript not enabled" />
</noscript>
然后检查您的访问日志,看看这个映像被击中了多少次。这是一个略显粗糙的解决方案,但它将为你的用户基础提供一个很好的百分比。
上面的方法(图像跟踪)不适用于纯文本浏览器或根本不支持js的浏览器,所以如果你的用户群主要倾向于这方面,这可能不是最好的方法。
在什么地方发现它?JavaScript ?那是不可能的。如果只是为了记录日志,可以使用某种跟踪方案,其中每个页面都有JavaScript,将对一个特殊资源(可能是一个非常小的gif或类似的文件)发出请求。这样,您就可以获取唯一页面请求和跟踪文件请求之间的差异。
一个常见的解决方案是将元标记与noscript结合起来刷新页面,并在JavaScript被禁用时通知服务器,如下所示:
<!DOCTYPE html>
<html lang="en">
<head>
<noscript>
<meta http-equiv="refresh" content="0; /?javascript=false">
</noscript>
<meta charset="UTF-8"/>
<title></title>
</head>
</html>
在上面的例子中,当JavaScript被禁用时,浏览器将在0秒内重定向到网站的主页。此外,它还会向服务器发送参数javascript=false。
然后,服务器端脚本(如node.js或PHP)可以解析该参数,并知道JavaScript已禁用。然后,它可以向客户端发送一个特殊的非javascript版本的网站。