今天早上有个帖子问有多少人禁用JavaScript。然后我开始想知道可以使用什么技术来确定用户是否禁用了它。
有人知道一些简单的方法来检测JavaScript是否被禁用吗?我的意图是给一个警告,如果浏览器没有启用JS,站点将无法正常运行。
最终,我想把它们重定向到能够在没有JS的情况下工作的内容,但我需要这个检测作为一个占位符来开始。
今天早上有个帖子问有多少人禁用JavaScript。然后我开始想知道可以使用什么技术来确定用户是否禁用了它。
有人知道一些简单的方法来检测JavaScript是否被禁用吗?我的意图是给一个警告,如果浏览器没有启用JS,站点将无法正常运行。
最终,我想把它们重定向到能够在没有JS的情况下工作的内容,但我需要这个检测作为一个占位符来开始。
当前回答
我昨天解决了同样的问题,所以我在这里加上。001。解决方案对我来说很有效至少在主页(index。php)
我希望根文件夹中只有一个文件:index.php。然后我使用文件夹来组织整个项目(代码,css, js等)。所以index.php的代码如下:
<head>
<title>Please Activate Javascript</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
</head>
<body>
<script language="JavaScript">
$(document).ready(function() {
location.href = "code/home.php";
});
</script>
<noscript>
<h2>This web site needs javascript activated to work properly. Please activate it. Thanks!</h2>
</noscript>
</body>
</html>
希望这对大家有所帮助。致以最亲切的问候。
其他回答
人们已经发布了一些例子,这些例子都是很好的检测选项,但是基于你的要求“给出警告,如果浏览器没有启用JS,站点将无法正常运行”。你可以添加一个元素,让它以某种方式出现在页面上,例如当你获得徽章时,在Stack Overflow上弹出相应的消息,然后用一些Javascript删除它,在页面加载时立即运行(我指的是DOM,而不是整个页面)。
一个常见的解决方案是将元标记与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版本的网站。
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禁用的总用户。每节课只检查一次,有以下好处:
访问100个页面或仅访问1个页面的用户每人算1个。这允许只关注单个用户,而不是页面。 不打破页面流,结构或语义在任何方式 可以记录用户代理的日志。这允许排除机器人统计,如谷歌机器人和bing机器人通常有JS禁用!也可以记录IP,时间等… 每个会话只检查一次(最小的过载)
我的代码使用PHP, mysql和jquery与ajax,但可以适应其他语言:
在你的数据库中创建一个像这样的表:
CREATE TABLE IF NOT EXISTS `log_JS` (
`logJS_id` int(11) NOT NULL AUTO_INCREMENT,
`data_ins` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`session_id` varchar(50) NOT NULL,
`JS_ON` tinyint(1) NOT NULL DEFAULT '0',
`agent` varchar(255) DEFAULT NULL,
PRIMARY KEY (`logJS_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
在使用session_start()或等效(jquery必需)后将此添加到每个页面:
<? if (!isset($_SESSION["JSTest"]))
{
mysql_query("INSERT INTO log_JS (session_id, agent) VALUES ('" . mysql_real_escape_string(session_id()) . "', '" . mysql_real_escape_string($_SERVER['HTTP_USER_AGENT']). "')");
$_SESSION["JSTest"] = 1; // One time per session
?>
<script type="text/javascript">
$(document).ready(function() { $.get('JSOK.php'); });
</script>
<?
}
?>
像这样创建页面JSOK.php:
<?
include_once("[DB connection file].php");
mysql_query("UPDATE log_JS SET JS_ON = 1 WHERE session_id = '" . mysql_real_escape_string(session_id()) . "'");
我昨天解决了同样的问题,所以我在这里加上。001。解决方案对我来说很有效至少在主页(index。php)
我希望根文件夹中只有一个文件:index.php。然后我使用文件夹来组织整个项目(代码,css, js等)。所以index.php的代码如下:
<head>
<title>Please Activate Javascript</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
</head>
<body>
<script language="JavaScript">
$(document).ready(function() {
location.href = "code/home.php";
});
</script>
<noscript>
<h2>This web site needs javascript activated to work properly. Please activate it. Thanks!</h2>
</noscript>
</body>
</html>
希望这对大家有所帮助。致以最亲切的问候。