今天早上有个帖子问有多少人禁用JavaScript。然后我开始想知道可以使用什么技术来确定用户是否禁用了它。
有人知道一些简单的方法来检测JavaScript是否被禁用吗?我的意图是给一个警告,如果浏览器没有启用JS,站点将无法正常运行。
最终,我想把它们重定向到能够在没有JS的情况下工作的内容,但我需要这个检测作为一个占位符来开始。
今天早上有个帖子问有多少人禁用JavaScript。然后我开始想知道可以使用什么技术来确定用户是否禁用了它。
有人知道一些简单的方法来检测JavaScript是否被禁用吗?我的意图是给一个警告,如果浏览器没有启用JS,站点将无法正常运行。
最终,我想把它们重定向到能够在没有JS的情况下工作的内容,但我需要这个检测作为一个占位符来开始。
当前回答
为什么需要知道服务器端是否启用了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的情况下索引您的站点。
其他回答
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,你可以这样做:
<form onsubmit="this.js_enabled.value=1;return true;">
<input type="hidden" name="js_enabled" value="0">
<input type="submit" value="go">
</form>
这将在提交表单之前将js_enabled的值更改为1。如果服务器端脚本得到0,则没有JS。如果它得到1,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();
我过去使用的一种技术是使用JavaScript编写一个会话cookie,它只是作为一个标志,表示启用了JavaScript。然后服务器端代码查找此cookie,如果没有找到,则采取适当的操作。当然,这种技术确实依赖于启用cookie !
这是为我工作:它重定向访问者如果javascript被禁用
<noscript><meta http-equiv="refresh" content="0; url=whatyouwant.html" /></noscript>