首页 > 代码库 > js用img代替ajax js心跳 向服务器定时传送参数 主要计算用户在线时长
js用img代替ajax js心跳 向服务器定时传送参数 主要计算用户在线时长
html:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>记录用户的在线时长</title>
</head>
<body>
</body>
</html>
<script type="text/javascript">
var Statistics_Website_logo ={
‘Website_logo_title‘:‘学而思‘,
‘Website_logo_Theme‘:‘教育行业‘,
‘Website_logo_Company‘:‘好未来‘
};
</script>
<script type="text/javascript" src="http://www.mamicode.com/js/OnlineTime.js"></script>
OnlineTime.js:
/***
****2015.1.4 img代替ajax心跳 主要计算用户在线时长****
***/
var xue = xue || {};
xue.infoCount = xue.infoCount || {};
xue.infoCount.TimerFunction = null;//定时器函数
xue.infoCount.RefreshTime = 5000;//定时器刷新时间
xue.infoCount.Domain = document.domain;//域名
xue.infoCount.URL = document.URL;//URL
xue.infoCount.Title = document.title;//页面标题
xue.infoCount.Resolution = (window.screen.height)+‘&&‘+(window.screen.width);//分辨率
xue.infoCount.ColorDepth = window.screen.colorDepth;//颜色深度
xue.infoCount.Referrer = document.referrer;//Referrer
xue.infoCount.ClientLanguage = navigator.language;//客户端语言
xue.infoCount.AppName = navigator.appName;//客户端浏览器名称
xue.infoCount.AppVersion = navigator.appVersion;//客户端浏览器版本号
xue.infoCount.Timestamp = new Date().getTime();//页面刷新时间戳
xue.infoCount.RefreshTimes = 0;//定时器响应的次数
xue.infoCount.ImgAjax = document.createElement("img");
xue.infoCount.ImgAjax.id="ImgOnlineTime";
xue.infoCount.ImgAjax.style.width = "0";
xue.infoCount.ImgAjax.style.height = "0";
document.body.appendChild(xue.infoCount.ImgAjax);
/****
***设置cookie和获取cookie***
***/
xue.infoCount.getsec = function(str){
var str1=str.substring(1,str.length)*1;
var str2=str.substring(0,1);
if (str2=="s"){
return str1*1000;
}else if (str2=="h"){
return str1*60*60*1000;
}else if (str2=="d"){
return str1*24*60*60*1000;
}
}
xue.infoCount.setCookie = function (name,value,time){
var strsec = this.getsec(time);
var exp = new Date();
exp.setTime(exp.getTime() + strsec*1);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
xue.infoCount.getCookie = function(name){
var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
if(arr=document.cookie.match(reg))
return (arr[2]);
else
return null;
}
//用户自定义的网站标识
xue.infoCount.Websitelogo = ‘‘;
var Statistics_Website_logo;
if(Statistics_Website_logo){
for (var Key in Statistics_Website_logo){
xue.infoCount.Websitelogo =xue.infoCount.Websitelogo+‘&‘+‘‘+Key+‘=‘+Statistics_Website_logo[Key]+‘‘;
}
}else{
xue.infoCount.Websitelogo = ‘‘;
}
/****
***统计页面函数***
***/
xue.infoCount.AjaxOnlineTime = function () {
this.RefreshTimes++;
this.setCookie("Visitorslogo","xueersi","d1000000000000000");//设置客户标识
this.Visitorslogo = this.getCookie(‘Visitorslogo‘);
if (this.Visitorslogo) {
this.Visitorslogo = ‘1‘
}else{
this.Visitorslogo = ‘0‘
}
this.Data = http://www.mamicode.com/‘http://xeslog.xesv5.com/?Domain=‘+this.Domain+‘&URL=‘+this.URL+‘&Title=‘+this.Title+‘&Resolution=‘+this.Resolution+‘&ColorDepth=‘+this.ColorDepth+‘&Referrer=‘+this.Referrer+‘&ClientLanguage=‘+this.ClientLanguage+‘&AppName=‘+this.AppName+‘&AppVersion=‘+this.AppVersion+‘&Timestamp=‘+this.Timestamp+‘&Visitorslogo=‘+this.Visitorslogo+‘‘+this.Websitelogo+‘‘;
document.getElementById(‘ImgOnlineTime‘).setAttribute("src",this.Data);
};
xue.infoCount.TimerFunction = window.setInterval("xue.infoCount.AjaxOnlineTime()", xue.infoCount.RefreshTime);//定时器每5s刷新一次
window.onload = function(){
window.onfocus=function(){
//window.clearInterval(xue.infoCount.TimerFunction);
xue.infoCount.TimerFunction = window.setInterval("xue.infoCount.AjaxOnlineTime()", xue.infoCount.RefreshTime);//定时器每5s刷新一次
};
window.onblur=function(){
window.clearInterval(xue.infoCount.TimerFunction);
};
};
js用img代替ajax js心跳 向服务器定时传送参数 主要计算用户在线时长