这篇文章介绍了JS精确统计网站访问量的实例代码
有需要的朋友可以参考一下
复制代码 代码如下:
/**
* vlstat 浏览器统计脚本
*/
var statIdName = "vlstatId";
var xmlHttp;
/**
* 设置cookieId
*/
function setCookie(c_name value expiredays) {
var exdate = new Date();
exdatesetDate(exdategetDate() + expiredays);
documentcookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdatetoGMTString()) + ";path=/;domain=cecbbcom";
}
/**
* 获取cookieId
*/
function getCookie(c_name) {
if (documentcookielength > ) {
c_start = documentcookieindexOf(c_name + "=");
if (c_start != ) {
c_start = c_start + c_namelength + ;
c_end = documentcookieindexOf(";" c_start);
if (c_end == ) {
c_end = documentcookielength;
}
return unescape(documentcookiesubstring(c_start c_end));
}
}
return "";
}
/**
* 获取当前时间戳
*/
function getTimestamp() {
var timestamp = Dateparse(new Date());
return timestamp;
}
/**
* 生成statId
*/
function genStatId() {
var cookieId = getTimestamp();
cookieId = "vlstat" + "" + cookieId + "" + Mathround(Mathrandom() * );
return cookieId;
}
/**
* 设置StatId
*/
function setStatId() {
var cookieId = genStatId();
setCookie(statIdName cookieId );
}
/**
* 获取StatId
*/
function getStatId() {
var statId = getCookie(statIdName);
if (statId != null && statIdlength > ) {
return statId;
} else {
setStatId();
return getStatId();
}
}
/**
* 获取UA
*/
function getUA() {
var ua = navigatoruserAgent;
if (ualength > ) {
ua = uasubstring( );
}
return ua;
}
/**
* 获取浏览器类型
*/
function getBrower() {
var ua = getUA();
if (uaindexOf("Maxthon") != ) {
return "Maxthon";
} else if (uaindexOf("MSIE") != ) {
return "MSIE";
} else if (uaindexOf("Firefox") != ) {
return "Firefox";
} else if (uaindexOf("Chrome") != ) {
return "Chrome";
} else if (uaindexOf("Opera") != ) {
return "Opera";
} else if (uaindexOf("Safari") != ) {
return "Safari";
} else {
return "ot";
}
}
/**
* 获取浏览器语言
*/
function getBrowerLanguage() {
var lang = navigatorbrowserLanguage;
return lang != null && langlength > ? lang : "";
}
/**
* 获取操作系统
*/
function getPlatform() {
return navigatorplatform;
}
/**
* 获取页面title
*/
function getPageTitle() {
return documenttitle;
}
/**
* 创建一个form
*
* @return
*/
function createSubmitForm() {
var frm = documentcreateElement("form");
documentbodyappendChild(frm);
frmmethod = "POST";
return frm;
}
/**
* 为form创建一个元素
*
* @param inputForm
* @param elementName
* @param elementValue
* @return
*/
function createFormElement(frmInput elementName elementValue) {
var element = documentcreateElement("input");
elementsetAttribute("id" elementName);
elementsetAttribute("name" elementName);
elementsetAttribute("type" "hidden");
elementsetAttribute("value" elementValue);
frmInputappendChild(element);
return element;
}
/**
* 构造XMLHttpRequest对象
*
* @return
*/
function createXMLHttpRequest() {
if (windowActiveXObject) {
xmlHttp = new ActiveXObject(MicrosoftXMLHTTP);
} else if (windowXMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
/**
* url指定跳转页data是要post的数据func类似于函数指针
*
* @param url
* @param data
* @param func
* @return
*/
function AjaxPost(url data func) {
var httpRequest = createHttpRequest();
if (httpRequest) {
("POST" url true);
("contentlength" datalength);
("ContentType" "application/xwwwformurlencoded");
(data);
() {
if {
try {
if {
if (func) {
// 这里可以调用想要的函数
func(
}
}
} catch (e) {
alert("Error XMLHttpRequest!");
}
}
}
} else {
alert("Error initializing XMLHttpRequest!");
}
}
function vlstatInitLE(vlch vlch vlch vlch) {
var p;
var vlstatCH = vlch != null && vlchlength > ? vlch : "";
var vlstatCH = vlch != null && vlchlength > ? vlch : "";
var vlstatCH = vlch != null && vlchlength > ? vlch : "";
var vlstatCH = vlch != null && vlchlength > ? vlch : "";
var vlstatCookieId = getStatId();
var vlstatUA = encodeURIComponent(getUA());
var vlstatIPAddress = documentlocalName;
var vlstatREFURL = encodeURIComponent(documentreferrer);
var vlstatURL = encodeURIComponent(documentURL);
var vlstatScreenX = screenwidth;
var vlstatScreenY = screenheight;
var vlstatOS = getPlatform();
var vlstatBrower = getBrower();
var vlstatBrowerLanguage = getBrowerLanguage();
var vlstatPageTitle = encodeURIComponent(getPageTitle());
var vlstatAction = "indexphp";
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 + "&ch=" + vlstatCH + "&ch=" + vlstatCH
+ "&ch=" + vlstatCH;
var urlGo = vlstatAction + "?" + p;
createXMLHttpRequest();
xmlHttpopen(GET urlGo);
xmlHttpsend(null);
}