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

作者:mrr 时间:2024-10-14 02:33:18 

网站一般都有访问量统计工具,比较高效实用的工具多种多样,并且非常的精确。实用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);
}
标签:js,网站,访问量,统计
0
投稿

猜你喜欢

  • PHP实现对数组分页处理实例详解

    2023-11-21 07:08:13
  • 在ASP.NET 2.0中操作数据之四:使用ObjectDataSource展现数据

    2024-05-13 09:15:45
  • python使用xpath获取页面元素的使用

    2021-04-30 10:32:09
  • PHP如何从txt文件中读取数据详解

    2023-11-15 02:37:32
  • Django中的文件的上传的几种方式

    2023-08-26 22:48:51
  • Python 合并拼接字符串的方法

    2022-05-26 08:58:08
  • 解决python中画图时x,y轴名称出现中文乱码的问题

    2023-08-04 09:00:23
  • python实现图片压缩代码实例

    2023-07-11 06:35:22
  • JavaScript自定义超时API代码实例

    2024-08-04 15:26:31
  • wxPython电子表格功能wx.grid实例教程

    2023-02-16 13:12:22
  • Django实现文件分享系统的完整代码

    2022-07-12 11:28:21
  • UCDChina.com 关于用户期望的讨论

    2008-07-10 11:55:00
  • python GUI库图形界面开发之PyQt5中QMainWindow, QWidget以及QDialog的区别和选择

    2022-04-22 01:39:50
  • Golang Socket Server自定义协议的简单实现方案

    2024-04-25 13:17:40
  • 利用CSS改善网站可访问性

    2010-10-20 20:12:00
  • 手动安装Anaconda环境变量的实现教程

    2023-12-09 07:10:27
  • 重新restore了mysql到另一台机器上后mysql 编码问题报错

    2024-01-24 05:10:41
  • python集合删除多种方法详解

    2021-12-06 07:59:51
  • python字典排序的方法

    2021-06-16 03:43:40
  • python模块的安装以及安装失败的解决方法

    2023-09-14 06:33:04
  • asp之家 网络编程 m.aspxhome.com