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="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://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);
    };
};

时间: 2024-08-04 15:56:21

js用img代替ajax js心跳 向服务器定时传送参数 主要计算用户在线时长的相关文章

Java程序员之JS(一) 之 JQuery.ajax

背景:紧着现在项目的需要,先从JQuery.ajax出发,主要需求是通过 js 调用Java 代码,从而适应现在的项目. 先从几个概念开始讲解: 一. 什么是Deferred  Deferred 对象是由.Deferred构造的, .Deferred被实现为简单工厂模式.它是用来解决JS中的异步编程,遵循 Common Promise/A规范,实现此规范的还有when.js 和 dojo. Deferred 对象在 JQuery 1.5被引入,用来解决 Ajax 异步优化问题,正是由于 Defe

【第2章第300回】原生JS与jQuery对AJAX的实现

一.定义 W3C里这么解释AJAX: AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML).AJAX 不是新的编程语言,而是一种使用现有标准的新方法.AJAX 是与服务器交换数据并更新部分网页的艺术,在不重新加载整个页面的情况下. 就是利用JS来无刷新与后端交互,通过get和post方式把数据发送到后端,或者请求后端的数据,然后根据请求的数据进行改变DOM节点等操作,从而取消掉用form的submit方式一提交就会跳转页面的情况

angular js 多处获取ajax数据的方法

angular js 多处获取ajax数据的方法 var app=angular.module("cart",[]);app.service("getData",function ($http) { return{ ajax:function () { return $http.get("product.json"); } }}); app.controller("listCtrl",function ($scope,getD

[js高手之路]Node.js+jade+mongoose实战todolist(分页,ajax编辑,删除)

该系列文章索引: [js高手之路]node js系列课程-创建简易web服务器与文件读写 [js高手之路]node js系列课程-图解express+supervisor+ejs用法 [js高手之路]Node.js实现简易的爬虫-抓取博客所有文章列表信息 [js高手之路]Node.js模板引擎教程-jade速学与实战1-基本用法 [js高手之路]Node.js模板引擎教程-jade速学与实战2-流程控制,转义与非转义 [js高手之路]Node.js模板引擎教程-jade速学与实战3-mixin

封装自用的脚本ajax.js

function createxmlhttp() { xmlhttpobj = false; try{//创建对象,一个一个的试,哎,要是能统一标准都好.. xmlhttpobj = new XMLHttpRequest; }catch(e){ try{ xmlhttpobj=new ActiveXObject("MSXML2.XMLHTTP"); }catch(e2){ try{ xmlhttpobj=new ActiveXObject("Microsoft.XMLHTTP

JS学习19(Ajax与Comet)

Ajax:Asynchronous JavaScript + XML的简写. Ajax技术的核心是XMLHttpRequest对象(XHR),XHR为向服务器发送请求和解析服务器响应提供了流畅的接口.能够以异步方式从服务器取得信息,再通过DOM将新数据插入到页面中. XMLHttpRequest对象 IE7+ Firefox Opera Chrome Safari原生支持XHR对象. var xhr = new XMLHttpRequest(); XHR的用法 在使用XHR对象时,要调用的第一个

JS案例之4——Ajax多图上传

近期项目中有好几次用到多图上传,第一次在项目中真正用到Ajax技术,稍微整理了下,贴个案例出来. 我们传统的做法是当用户提交一个表单时,就向web服务器端发送一个请求.服务器接受并处理传来的表单信息,处理完成后返回一个新的页面.这个做法比较浪费带宽,当请求数较多时,页面响应的时间就依赖于服务器处理的时间. 而Ajax应用仅向服务器发送并取回必需的数据,其他不需要的数据不用响应,它使用SOAP或其它一些基于XML的web service接口,并在客户端采用JS来处理来自服务器的响应.因此在服务器和

C#MVC和cropper.js实现剪裁图片ajax上传的弹出层

 首先使用cropper.js插件,能够将剪裁后的图片返回为base64编码,后台根据base64编码解析保存图片. jQuery.cropper: 是一款使用简单且功能强大的图片剪裁jquery插件.该图片剪裁插件支持图片放大缩小,支持图片旋转,支持触摸屏设备,支持canvas,并且支持跨浏览器使用 网站:http://fengyuanchen.github.io/cropper/ 可以自己搜索中文API 前台代码: @{ Layout = null; } <!DOCTYPE html> &

城市四级联动,js和jquery对ajax的支持结合html,json

                                                                 jdbc工具类                                                       public class JdbcUtils { public static BasicDataSource bs=null; static{ Properties p = new Properties(); try { p.load(JdbcU