JS实现在线统计一个页面内鼠标点击次数的方法
编程学习 2021-07-04 21:47www.dzhlxh.cn编程入门
这篇文章主要介绍了JS实现在线统计一个页面内鼠标点击次数的方法,实例分析了javascript操作Cookie实现计数的技巧,需要的朋友可以参考下
本文实例讲述了JS实现在线统计一个页面内鼠标点击次数的方法。分享给大家供大家参考。具体实现方法如下:
代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>js记录鼠标的点击次数</title>
<meta http-equiv="content-type" content="text/html;charset=gb2312">
</head>
<body>
<div id="count"></div>狼蚁SEO www.jb51.net 提示您:
<script type="text/javascript">
function addCookie(name,cookievalue,time) {
if (name != "" && cookievalue != "" && time != "") {
if (isNaN(time) == false){
var expires = new Date();
expires.setTime(expires.getTime() + time * 1000);
document.cookie = name + '=' + escape(cookievalue) + ';expires=' + expires.toGMTString();
}
}
}
function getCookie(cookieName) {
var cookieString = document.cookie;
var start = cookieString.indexOf(cookieName + '=');
if (start == -1)
return null;
start += cookieName.length + 1;
var end = cookieString.indexOf(';', start);
if (end == -1) return unescape(cookieString.substring(start));
return unescape(cookieString.substring(start, end));
}
var html = document.getElementsByTagName("html")[0];
html.onclick = function(){
var count = parseInt(getCookie('count'))+1;
addCookie("count",count,"1000");
document.getElementById("count").innerHTML = "您点击了"+getCookie('count')+"次!";
}
if (getCookie('count')){
document.getElementById("count").innerHTML = "您点击了"+getCookie('count')+"次!";
}else{
document.getElementById("count").innerHTML = "您还没有点击过!";
addCookie("count","0","1000");
}
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>js记录鼠标的点击次数</title>
<meta http-equiv="content-type" content="text/html;charset=gb2312">
</head>
<body>
<div id="count"></div>狼蚁SEO www.jb51.net 提示您:
<script type="text/javascript">
function addCookie(name,cookievalue,time) {
if (name != "" && cookievalue != "" && time != "") {
if (isNaN(time) == false){
var expires = new Date();
expires.setTime(expires.getTime() + time * 1000);
document.cookie = name + '=' + escape(cookievalue) + ';expires=' + expires.toGMTString();
}
}
}
function getCookie(cookieName) {
var cookieString = document.cookie;
var start = cookieString.indexOf(cookieName + '=');
if (start == -1)
return null;
start += cookieName.length + 1;
var end = cookieString.indexOf(';', start);
if (end == -1) return unescape(cookieString.substring(start));
return unescape(cookieString.substring(start, end));
}
var html = document.getElementsByTagName("html")[0];
html.onclick = function(){
var count = parseInt(getCookie('count'))+1;
addCookie("count",count,"1000");
document.getElementById("count").innerHTML = "您点击了"+getCookie('count')+"次!";
}
if (getCookie('count')){
document.getElementById("count").innerHTML = "您点击了"+getCookie('count')+"次!";
}else{
document.getElementById("count").innerHTML = "您还没有点击过!";
addCookie("count","0","1000");
}
</script>
</body>
</html>
希望本文所述对大家的javascript程序设计有所帮助。