javascript实现的网站访问量统计代码


Posted in Javascript onDecember 20, 2015

网站一般都有访问量统计工具,比较高效实用的工具多种多样,并且非常的精确。实用javascript也可以简单的实现此功能,尽管没有网络上常用的精确,不过的确也实现了一定的功能,下面就是一段代码实例,感兴趣的朋友可以参考一下:

废话不多说了,直接给大家贴js代码了。

<script type="text/javascript">
/**
* vlstat 浏览器统计脚本
*/
var statIdName = "vlstatId";
var xmlHttp;
/**
* 设置cookieId
*/
function setCookie(c_name, value, expiredays) {
 var exdate = new Date();
 exdate.setDate(exdate.getDate() + expiredays);
 document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()) + ";path=/;domain=cecb2b.com";
}
/**
* 获取cookieId
*/
function getCookie(c_name) {
 if (document.cookie.length > 0) {
  c_start = document.cookie.indexOf(c_name + "=");
  if (c_start != -1) {
   c_start = c_start + c_name.length + 1;
   c_end = document.cookie.indexOf(";", c_start);
   if (c_end == -1) {
    c_end = document.cookie.length;
   }
   return unescape(document.cookie.substring(c_start, c_end));
  }
 }
 return "";
}
/**
* 获取当前时间戳
*/
function getTimestamp() {
 var timestamp = Date.parse(new Date());
 return timestamp;
}
/**
* 生成statId
*/
function genStatId() {
 var cookieId = getTimestamp();
 cookieId = "vlstat" + "-" + cookieId + "-" + Math.round(Math.random() * 3000000000);
 return cookieId;
}
/**
* 设置StatId
*/
function setStatId() {
 var cookieId = genStatId();
 setCookie(statIdName, cookieId, 365);
}
/**
* 获取StatId
*/
function getStatId() {
 var statId = getCookie(statIdName);
 if (statId != null && statId.length > 0) {
  return statId;
 } else {
  setStatId();
  return getStatId();
 }
}
/**
* 获取UA
*/
function getUA() {
 var ua = navigator.userAgent;
 if (ua.length > 250) {
  ua = ua.substring(0, 250);
 }
 return ua;
}
/**
* 获取浏览器类型
*/
function getBrower() {
 var ua = getUA();
 if (ua.indexOf("Maxthon") != -1) {
  return "Maxthon";
 } else if (ua.indexOf("MSIE") != -1) {
  return "MSIE";
 } else if (ua.indexOf("Firefox") != -1) {
  return "Firefox";
 } else if (ua.indexOf("Chrome") != -1) {
  return "Chrome";
 } else if (ua.indexOf("Opera") != -1) {
  return "Opera";
 } else if (ua.indexOf("Safari") != -1) {
  return "Safari";
 } else {
  return "ot";
 }
}
/**
* 获取浏览器语言
*/
function getBrowerLanguage() {
 var lang = navigator.browserLanguage;
 return lang != null && lang.length > 0 ? lang : "";
}
/**
* 获取操作系统
*/
function getPlatform() {
 return navigator.platform;
}
/**
* 获取页面title
*/
function getPageTitle() {
 return document.title;
}
/**
* 创建一个form
* 
* @return
*/
function createSubmitForm() {
 var frm = document.createElement("form");
 document.body.appendChild(frm);
 frm.method = "POST";
 return frm;
}
/**
* 为form创建一个元素
* 
* @param inputForm
* @param elementName
* @param elementValue
* @return
*/
function createFormElement(frmInput, elementName, elementValue) {
 var element = document.createElement("input");
 element.setAttribute("id", elementName);
 element.setAttribute("name", elementName);
 element.setAttribute("type", "hidden");
 element.setAttribute("value", elementValue);
 frmInput.appendChild(element);
 return element;
}
/**
* 构造XMLHttpRequest对象
* 
* @return
*/
function createXMLHttpRequest() { 
if (window.ActiveXObject) { 
 xmlHttp = new ActiveXObject('Microsoft.XMLHTTP'); 
} else if (window.XMLHttpRequest) { 
 xmlHttp = new XMLHttpRequest(); 
} 
}
/**
* url指定跳转页,data是要post的数据。func类似于函数指针
* 
* @param url
* @param data
* @param func
* @return
*/
function AjaxPost(url, data, func) {
 var httpRequest = createHttpRequest();
 if (httpRequest) {
  httpRequest.open("POST", url, true);
  httpRequest.setRequestHeader("content-length", data.length);
  httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  httpRequest.send(data);
  httpRequest.onreadystatechange = function() {
   if (httpRequest.readyState == 4) {
    try {
     if (httpRequest.status == 200) {
      if (func) {
       // 这里可以调用想要的函数
       func(httpRequest.responseText);
      }
     }
    } catch (e) {
     alert("Error XMLHttpRequest!");
    }
   }
  }
 } else {
  alert("Error initializing XMLHttpRequest!");
 }
}
function vlstatInitLE(vlch, vlch1, vlch2, vlch3) {
 var p;
 var vlstatCH = vlch != null && vlch.length > 0 ? vlch : "";
 var vlstatCH1 = vlch1 != null && vlch1.length > 0 ? vlch1 : "";
 var vlstatCH2 = vlch2 != null && vlch2.length > 0 ? vlch2 : "";
 var vlstatCH3 = vlch3 != null && vlch3.length > 0 ? vlch3 : "";
 var vlstatCookieId = getStatId();
 var vlstatUA = encodeURIComponent(getUA());
 var vlstatIPAddress = document.localName;
 var vlstatREFURL = encodeURIComponent(document.referrer);
 var vlstatURL = encodeURIComponent(document.URL);
 var vlstatScreenX = screen.width;
 var vlstatScreenY = screen.height;
 var vlstatOS = getPlatform();
 var vlstatBrower = getBrower();
 var vlstatBrowerLanguage = getBrowerLanguage();
 var vlstatPageTitle = encodeURIComponent(getPageTitle());
 var vlstatAction = "index.php";
 p = "cookieId=" + vlstatCookieId + "&ua=" + vlstatUA + "&ip=" + vlstatIPAddress + "&refurl="
   + vlstatREFURL + "&url=" + vlstatURL + "&screenX=" + vlstatScreenX + "&screenY=" + vlstatScreenY
   + "&os=" + vlstatOS + "&brower=" + vlstatBrower + "&browerLang=" + vlstatBrowerLanguage
   + "&title=" + vlstatPageTitle + "&ch=" + vlstatCH + "&ch1=" + vlstatCH1 + "&ch2=" + vlstatCH2
   + "&ch3=" + vlstatCH3;
 var urlGo = vlstatAction + "?" + p;
 createXMLHttpRequest();
xmlHttp.open('GET', urlGo);
xmlHttp.send(null);
}
</script>

下面在给大家分享一段代码实例-JS 精确统计网站访问量程序

/** 
 * vlstat 浏览器统计脚本 
 */ 
var statIdName = "vlstatId"; 
var xmlHttp; 
/** 
 * 设置cookieId 
 */ 
function setCookie(c_name, value, expiredays) { 
  var exdate = new Date(); 
  exdate.setDate(exdate.getDate() + expiredays); 
  document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()) + ";path=/;domain=cecb2b.com"; 
} 
/** 
 * 获取cookieId 
 */ 
function getCookie(c_name) { 
  if (document.cookie.length > 0) { 
    c_start = document.cookie.indexOf(c_name + "="); 
    if (c_start != -1) { 
      c_start = c_start + c_name.length + 1; 
      c_end = document.cookie.indexOf(";", c_start); 
      if (c_end == -1) { 
        c_end = document.cookie.length; 
      } 
      return unescape(document.cookie.substring(c_start, c_end)); 
    } 
  } 
  return ""; 
} 
/** 
 * 获取当前时间戳 
 */ 
function getTimestamp() { 
  var timestamp = Date.parse(new Date()); 
  return timestamp; 
} 
/** 
 * 生成statId 
 */ 
function genStatId() { 
  var cookieId = getTimestamp(); 
  cookieId = "vlstat" + "-" + cookieId + "-" + Math.round(Math.random() * 3000000000); 
  return cookieId; 
} 
/** 
 * 设置StatId 
 */ 
function setStatId() { 
  var cookieId = genStatId(); 
  setCookie(statIdName, cookieId, 365); 
} 
/** 
 * 获取StatId 
 */ 
function getStatId() { 
  var statId = getCookie(statIdName); 
  if (statId != null && statId.length > 0) { 
    return statId; 
  } else { 
    setStatId(); 
    return getStatId(); 
  } 
} 
/** 
 * 获取UA 
 */ 
function getUA() { 
  var ua = navigator.userAgent; 
  if (ua.length > 250) { 
    ua = ua.substring(0, 250); 
  } 
  return ua; 
} 
/** 
 * 获取浏览器类型 
 */ 
function getBrower() { 
  var ua = getUA(); 
  if (ua.indexOf("Maxthon") != -1) { 
    return "Maxthon"; 
  } else if (ua.indexOf("MSIE") != -1) { 
    return "MSIE"; 
  } else if (ua.indexOf("Firefox") != -1) { 
    return "Firefox"; 
  } else if (ua.indexOf("Chrome") != -1) { 
    return "Chrome"; 
  } else if (ua.indexOf("Opera") != -1) { 
    return "Opera"; 
  } else if (ua.indexOf("Safari") != -1) { 
    return "Safari"; 
  } else { 
    return "ot"; 
  } 
} 
/** 
 * 获取浏览器语言 
 */ 
function getBrowerLanguage() { 
  var lang = navigator.browserLanguage; 
  return lang != null && lang.length > 0 ? lang : ""; 
} 
/** 
 * 获取操作系统 
 */ 
function getPlatform() { 
  return navigator.platform; 
} 
/** 
 * 获取页面title 
 */ 
function getPageTitle() { 
  return document.title; 
} 
/** 
 * 创建一个form 
 * 
 * @return 
 */ 
function createSubmitForm() { 
  var frm = document.createElement("form"); 
  document.body.appendChild(frm); 
  frm.method = "POST"; 
  return frm; 
} 
/** 
 * 为form创建一个元素 
 * 
 * @param inputForm 
 * @param elementName 
 * @param elementValue 
 * @return 
 */ 
function createFormElement(frmInput, elementName, elementValue) { 
  var element = document.createElement("input"); 
  element.setAttribute("id", elementName); 
  element.setAttribute("name", elementName); 
  element.setAttribute("type", "hidden"); 
  element.setAttribute("value", elementValue); 
  frmInput.appendChild(element); 
  return element; 
} 
/** 
 * 构造XMLHttpRequest对象 
 * 
 * @return 
 */ 
function createXMLHttpRequest() {  
  if (window.ActiveXObject) {  
    xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');  
  } else if (window.XMLHttpRequest) {  
    xmlHttp = new XMLHttpRequest();  
  }  
} 
/** 
 * url指定跳转页,data是要post的数据。func类似于函数指针 
 * 
 * @param url 
 * @param data 
 * @param func 
 * @return 
 */ 
function AjaxPost(url, data, func) { 
  var httpRequest = createHttpRequest(); 
  if (httpRequest) { 
    httpRequest.open("POST", url, true); 
    httpRequest.setRequestHeader("content-length", data.length); 
    httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
    httpRequest.send(data); 
    httpRequest.onreadystatechange = function() { 
      if (httpRequest.readyState == 4) { 
        try { 
          if (httpRequest.status == 200) { 
            if (func) { 
              // 这里可以调用想要的函数 
              func(httpRequest.responseText); 
            } 
          } 
        } catch (e) { 
          alert("Error XMLHttpRequest!"); 
        } 
      } 
    } 
  } else { 
    alert("Error initializing XMLHttpRequest!"); 
  } 
} 
function vlstatInitLE(vlch, vlch1, vlch2, vlch3) { 
  var p; 
  var vlstatCH = vlch != null && vlch.length > 0 ? vlch : ""; 
  var vlstatCH1 = vlch1 != null && vlch1.length > 0 ? vlch1 : ""; 
  var vlstatCH2 = vlch2 != null && vlch2.length > 0 ? vlch2 : ""; 
  var vlstatCH3 = vlch3 != null && vlch3.length > 0 ? vlch3 : ""; 
  var vlstatCookieId = getStatId(); 
  var vlstatUA = encodeURIComponent(getUA()); 
  var vlstatIPAddress = document.localName; 
  var vlstatREFURL = encodeURIComponent(document.referrer); 
  var vlstatURL = encodeURIComponent(document.URL); 
  var vlstatScreenX = screen.width; 
  var vlstatScreenY = screen.height; 
  var vlstatOS = getPlatform(); 
  var vlstatBrower = getBrower(); 
  var vlstatBrowerLanguage = getBrowerLanguage(); 
  var vlstatPageTitle = encodeURIComponent(getPageTitle()); 
  var vlstatAction = "index.php"; 
  p = "cookieId=" + vlstatCookieId + "&ua=" + vlstatUA + "&ip=" + vlstatIPAddress + "&refurl=" 
      + vlstatREFURL + "&url=" + vlstatURL + "&screenX=" + vlstatScreenX + "&screenY=" + vlstatScreenY 
      + "&os=" + vlstatOS + "&brower=" + vlstatBrower + "&browerLang=" + vlstatBrowerLanguage 
      + "&title=" + vlstatPageTitle + "&ch=" + vlstatCH + "&ch1=" + vlstatCH1 + "&ch2=" + vlstatCH2 
      + "&ch3=" + vlstatCH3; 
  var urlGo = vlstatAction + "?" + p; 
  createXMLHttpRequest(); 
  xmlHttp.open('GET', urlGo); 
  xmlHttp.send(null); 
}
Javascript 相关文章推荐
prototype 1.5 &amp; scriptaculous 1.6.1 学习笔记
Sep 07 Javascript
javascript开发随笔二 动态加载js和文件
Nov 25 Javascript
javascript函数作用域学习示例(js作用域)
Jan 13 Javascript
javascript下拉列表菜单的实现方法
Nov 18 Javascript
JavaScript中实现无缝滚动、分享到侧边栏实例代码
Apr 06 Javascript
React-Native 组件之 Modal的使用详解
Aug 08 Javascript
JS实现获取汉字首字母拼音、全拼音及混拼音的方法
Nov 14 Javascript
JS交互点击WKWebView中的图片实现预览效果
Jan 05 Javascript
vue实现验证码按钮倒计时功能
Apr 10 Javascript
vue.js前后端数据交互之提交数据操作详解
Apr 24 Javascript
详解实现一个通用的“划词高亮”在线笔记功能
Apr 23 Javascript
微信小程序request请求封装,验签代码实例
Dec 04 Javascript
js实现n秒倒计时后才可以点击的效果
Dec 20 #Javascript
图解js图片轮播效果
Dec 20 #Javascript
谈谈我对JavaScript原型和闭包系列理解(随手笔记6)
Dec 20 #Javascript
基于jquery实现页面滚动到底自动加载数据的功能
Dec 19 #Javascript
js点击按钮实现带遮罩层的弹出视频效果
Dec 19 #Javascript
js获取本机操作系统类型的两种方法
Dec 19 #Javascript
javascript精确统计网站访问量实例代码
Dec 19 #Javascript
You might like
PHP利用APC模块实现文件上传进度条的方法
2015/01/26 PHP
PHP 生成微信红包代码简单
2016/03/25 PHP
html中的input标签的checked属性jquery判断代码
2012/09/19 Javascript
利用NodeJS的子进程(child_process)调用系统命令的方法分享
2013/06/05 NodeJs
js判读浏览器是否支持html5的canvas的代码
2013/11/18 Javascript
javascript实现带节日和农历的日历特效
2015/02/01 Javascript
JavaScript使用delete删除数组元素用法示例【数组长度不变】
2017/01/17 Javascript
JS实现动态给标签控件添加事件的方法示例
2017/05/13 Javascript
AngularJS使用ng-class动态增减class样式的方法示例
2017/05/18 Javascript
JavaScript 用fetch 实现异步下载文件功能
2017/07/21 Javascript
简单实现js放大镜效果
2017/07/24 Javascript
bmob js-sdk 在vue中的使用教程
2018/01/21 Javascript
Angular 4.x+Ionic3踩坑之Ionic 3.x界面传值详解
2018/03/13 Javascript
详解vuex中mapState,mapGetters,mapMutations,mapActions的作用
2018/04/13 Javascript
JS实现中英文混合文字溢出友好截取功能
2018/08/06 Javascript
更强大的vue ssr实现预取数据的方式
2019/07/19 Javascript
微信小程序日历插件代码实例
2019/12/04 Javascript
[01:42]辉夜杯战队访谈宣传片—FANTUAN
2015/12/25 DOTA
pyqt4教程之messagebox使用示例分享
2014/03/07 Python
python常用web框架简单性能测试结果分享(包含django、flask、bottle、tornado)
2014/08/25 Python
python中对list去重的多种方法
2014/09/18 Python
python3基于OpenCV实现证件照背景替换
2018/07/18 Python
Python socket实现的简单通信功能示例
2018/08/21 Python
Python一句代码实现找出所有水仙花数的方法
2018/11/13 Python
python里dict变成list实例方法
2019/06/26 Python
深入了解NumPy 高级索引
2020/07/24 Python
Django xadmin安装及使用详解
2020/10/26 Python
利用Storage Event实现页面间通信的示例代码
2018/07/26 HTML / CSS
Auguste The Label官网:澳大利亚一家精品女装时尚品牌
2020/06/14 全球购物
资深生产主管自我评价
2013/09/22 职场文书
结婚邀请函范文
2014/01/14 职场文书
奉献演讲稿范文
2014/05/21 职场文书
消防安全宣传标语
2014/06/07 职场文书
奥林匹克运动会口号
2014/06/19 职场文书
工程资料员岗位职责
2015/04/13 职场文书
浙江省杭州市平均工资标准是多少?
2019/07/09 职场文书