javascript實(shí)現(xiàn)的網(wǎng)站訪問量統(tǒng)計(jì)代碼
網(wǎng)站一般都有訪問量統(tǒng)計(jì)工具,比較高效實(shí)用的工具多種多樣,并且非常的精確。實(shí)用javascript也可以簡單的實(shí)現(xiàn)此功能,盡管沒有網(wǎng)絡(luò)上常用的精確,不過的確也實(shí)現(xiàn)了一定的功能,下面就是一段代碼實(shí)例,感興趣的朋友可以參考一下:
廢話不多說了,直接給大家貼js代碼了。
<script type="text/javascript">
/**
* vlstat 瀏覽器統(tǒng)計(jì)腳本
*/
var statIdName = "vlstatId";
var xmlHttp;
/**
* 設(shè)置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 "";
}
/**
* 獲取當(dāng)前時間戳
*/
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;
}
/**
* 設(shè)置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 : "";
}
/**
* 獲取操作系統(tǒng)
*/
function getPlatform() {
return navigator.platform;
}
/**
* 獲取頁面title
*/
function getPageTitle() {
return document.title;
}
/**
* 創(chuàng)建一個form
*
* @return
*/
function createSubmitForm() {
var frm = document.createElement("form");
document.body.appendChild(frm);
frm.method = "POST";
return frm;
}
/**
* 為form創(chuàng)建一個元素
*
* @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;
}
/**
* 構(gòu)造XMLHttpRequest對象
*
* @return
*/
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
} else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
/**
* url指定跳轉(zhuǎn)頁,data是要post的數(shù)據(jù)。func類似于函數(shù)指針
*
* @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) {
// 這里可以調(diào)用想要的函數(shù)
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>
下面在給大家分享一段代碼實(shí)例-JS 精確統(tǒng)計(jì)網(wǎng)站訪問量程序
/**
* vlstat 瀏覽器統(tǒng)計(jì)腳本
*/
var statIdName = "vlstatId";
var xmlHttp;
/**
* 設(shè)置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 "";
}
/**
* 獲取當(dāng)前時間戳
*/
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;
}
/**
* 設(shè)置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 : "";
}
/**
* 獲取操作系統(tǒng)
*/
function getPlatform() {
return navigator.platform;
}
/**
* 獲取頁面title
*/
function getPageTitle() {
return document.title;
}
/**
* 創(chuàng)建一個form
*
* @return
*/
function createSubmitForm() {
var frm = document.createElement("form");
document.body.appendChild(frm);
frm.method = "POST";
return frm;
}
/**
* 為form創(chuàng)建一個元素
*
* @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;
}
/**
* 構(gòu)造XMLHttpRequest對象
*
* @return
*/
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
} else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
/**
* url指定跳轉(zhuǎn)頁,data是要post的數(shù)據(jù)。func類似于函數(shù)指針
*
* @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) {
// 這里可以調(diào)用想要的函數(shù)
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);
}
相關(guān)文章
JavaScript 模式之工廠模式(Factory)應(yīng)用介紹
工廠模式也是對象創(chuàng)建模式之一,它通常在類或類的靜態(tài)方法中去實(shí)現(xiàn),本文將詳細(xì)介紹JavaScript 工廠模式2012-11-11
CKEditor擴(kuò)展插件:自動排版功能autoformat插件實(shí)現(xiàn)方法詳解
這篇文章主要介紹了CKEditor擴(kuò)展插件:自動排版功能autoformat插件實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了CKEditor擴(kuò)展插件實(shí)現(xiàn)自動排版功能的autoformat插件具體定義、配置與使用技巧,需要的朋友可以參考下2020-02-02
你必須知道的Javascript知識點(diǎn)之"單線程事件驅(qū)動"的使用
本篇文章小編為大家介紹,你必須知道的Javascript知識點(diǎn)之"單線程事件驅(qū)動"的使用。需要的朋友參考下2013-04-04
js實(shí)現(xiàn)帶緩動動畫的導(dǎo)航欄效果
本篇文章主要分享了js實(shí)現(xiàn)帶緩動動畫的導(dǎo)航欄效果的示例代碼。具有一定的參考價值,下面跟著小編一起來看下吧2017-01-01
?javascript學(xué)數(shù)組中的foreach方法和some方法
這篇文章主要介紹了?javascript學(xué)數(shù)組中的foreach方法和some方法,文章相關(guān)內(nèi)容和代碼詳細(xì),具有一定的參考價值,需要的小伙伴可以參考一下,希望對你的學(xué)習(xí)有所幫助2022-03-03

