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 相关文章推荐
奉献给JavaScript初学者的编写开发的七个细节
Jan 11 Javascript
使用JavaScript实现连续滚动字幕效果的方法
Jul 07 Javascript
JavaScript仿静态分页实现方法
Aug 04 Javascript
详解Bootstrap的aria-label和aria-labelledby应用
Jan 04 Javascript
利用Vue.js指令实现全选功能
Sep 08 Javascript
原生JS实现图片翻书效果
Feb 16 Javascript
SVG动画vivus.js库使用小结(实例代码)
Sep 14 Javascript
一个简易时钟效果js实现代码
Mar 25 Javascript
基于vue 动态加载图片src的解决方法
Feb 05 Javascript
浅析node.js的模块加载机制
May 25 Javascript
clipboard.js在移动端复制失败的解决方法
Jun 13 Javascript
微信小程序用户位置权限的获取方法(拒绝后提醒)
Nov 15 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
Zend Framework入门教程之Zend_View组件用法示例
2016/12/09 PHP
php微信公众号开发之校园图书馆
2018/10/20 PHP
asp函数split()对应php函数explode()
2019/02/27 PHP
js 动态文字滚动的例子
2011/01/17 Javascript
Knockout text绑定DOM的使用方法
2013/11/15 Javascript
nodejs的10个性能优化技巧
2014/07/15 NodeJs
javascript实现节点(div)名称编辑
2014/12/17 Javascript
js调用webservice构造SOAP进行身份验证
2016/04/27 Javascript
html、css和jquery相结合实现简单的进度条效果实例代码
2016/10/24 Javascript
Vue.js开发环境搭建
2016/11/10 Javascript
JS遍历对象属性的方法示例
2017/01/10 Javascript
基于bootstrap实现收缩导航条
2017/03/17 Javascript
微信小程序 自定义Toast实例代码
2017/06/12 Javascript
JavaScript实现图片懒加载的方法分析
2018/07/05 Javascript
React中嵌套组件与被嵌套组件的通信过程
2018/07/11 Javascript
js正则取值的结果数组调试方法
2018/10/10 Javascript
[00:53]TI3正赛第三天 DK怒破A队不败金身 现场国旗飘扬热血激昂
2013/08/10 DOTA
pycharm 使用心得(三)Hello world!
2014/06/05 Python
浅析python中的分片与截断序列
2016/08/09 Python
朴素贝叶斯分类算法原理与Python实现与使用方法案例
2018/06/26 Python
Django之路由层的实现
2019/09/09 Python
如何分离django中的媒体、静态文件和网页
2019/11/12 Python
Python3爬虫ChromeDriver的安装实例
2021/02/06 Python
对CSS3选择器的研究(详解)
2016/09/16 HTML / CSS
Linux内核产生并发的原因
2012/07/13 面试题
中软国际Java程序员机试题
2012/08/19 面试题
会计自我鉴定范文
2013/10/06 职场文书
不假外出检讨书
2014/01/27 职场文书
我是一名护士演讲稿
2014/08/28 职场文书
解除劳动合同证明书模板
2014/11/20 职场文书
小学四年级学生评语
2014/12/26 职场文书
Python打包为exe详细教程
2021/05/18 Python
分享7个 Python 实战项目练习
2022/03/03 Python
Python借助with语句实现代码段只执行有限次
2022/03/23 Python
MySQL运行报错:“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”解决方法
2022/06/14 MySQL
Java中的Kafka为什么性能这么快及4大核心详析
2022/09/23 Java/Android