萬盛學電腦網

 萬盛學電腦網 >> 腳本專題 >> javascript >> JS 精確統計網站訪問量的實例代碼

JS 精確統計網站訪問量的實例代碼

這篇文章介紹了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 {
                  &nbs

copyright © 萬盛學電腦網 all rights reserved