今天早上有个帖子问有多少人禁用JavaScript。然后我开始想知道可以使用什么技术来确定用户是否禁用了它。
有人知道一些简单的方法来检测JavaScript是否被禁用吗?我的意图是给一个警告,如果浏览器没有启用JS,站点将无法正常运行。
最终,我想把它们重定向到能够在没有JS的情况下工作的内容,但我需要这个检测作为一个占位符来开始。
今天早上有个帖子问有多少人禁用JavaScript。然后我开始想知道可以使用什么技术来确定用户是否禁用了它。
有人知道一些简单的方法来检测JavaScript是否被禁用吗?我的意图是给一个警告,如果浏览器没有启用JS,站点将无法正常运行。
最终,我想把它们重定向到能够在没有JS的情况下工作的内容,但我需要这个检测作为一个占位符来开始。
当前回答
我已经想出了另一种方法使用css和javascript本身。 这只是开始修补类和id。
CSS代码片段: 1. 创建一个css ID规则,命名为#jsDis。 2. 使用"content"属性在BODY元素后生成文本。(你可以按照自己的意愿设置样式)。 3创建第二个css ID规则,命名为#jsEn,并对其进行风格化。(为了简单起见,我给#jsEn规则设置了不同的背景色。
<style>
#jsDis:after {
content:"Javascript is Disable. Please turn it ON!";
font:bold 11px Verdana;
color:#FF0000;
}
#jsEn {
background-color:#dedede;
}
#jsEn:after {
content:"Javascript is Enable. Well Done!";
font:bold 11px Verdana;
color:#333333;
}
</style>
JavaScript代码片段: 1. 创建一个函数。 2. 使用getElementById获取BODY ID并将其分配给一个变量。 3.使用JS函数'setAttribute',改变BODY元素ID属性的值。
<script>
function jsOn() {
var chgID = document.getElementById('jsDis');
chgID.setAttribute('id', 'jsEn');
}
</script>
HTML部分。 1. 将BODY元素属性的ID命名为#jsDis。 2. 使用函数名添加onLoad事件。(jsOn())。
<body id="jsDis" onLoad="jsOn()">
因为BODY标签的ID是#jsDis: -如果Javascript被启用,它会自己改变BODY标签的属性。 -如果Javascript是禁用的,它将显示css 'content:'规则文本。
你可以使用#包装容器,或者使用任何使用JS的DIV。 希望这有助于理解。
其他回答
一个常见的解决方案是将元标记与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版本的网站。
我认为你可以在noscript标签中插入一个图像标签,并查看你的站点有多少次以及这个图像被加载的频率。
您会想看一下noscript标记。
<script type="text/javascript">
...some javascript script to insert data...
</script>
<noscript>
<p>Access the <a href="http://someplace.com/data">data.</a></p>
</noscript>
如果javascript被禁用,您的客户端代码将无法运行,所以我假设您的意思是您希望该信息可用的服务器端。在这种情况下,noscript就没那么有用了。相反,我有一个隐藏的输入,并使用javascript填充一个值。在你的下一个请求或回发后,如果值在那里,你知道javascript是打开的。
注意像noscript这样的事情,第一次请求可能显示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...