這支統計腳本的核心邏輯是:在瀏覽器端採集環境資訊,組成查詢字串後以 GET 方式回報給伺服器端接收頁。因為資料都在客戶端取得,伺服器端只需負責落檔與統計。
var statIdName = "vlstatId";
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=example.com";
}
function getCookie(c_name) {
if (document.cookie.length > 0) {
var c_start = document.cookie.indexOf(c_name + "=");
if (c_start != -1) {
c_start = c_start + c_name.length + 1;
var 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 genStatId() {
var ts = Date.parse(new Date());
return "vlstat-" + ts + "-" + Math.round(Math.random() * 3000000000);
}
function getStatId() {
var statId = getCookie(statIdName);
if (statId != null && statId.length > 0) {
return statId;
} else {
setCookie(statIdName, genStatId(), 365);
return getStatId();
}
}
ID 由時間戳加上隨機數組成,寫入有效期一年的 Cookie。同一訪客重複造訪時沿用同一個 ID,這正是「精確統計」有別於單純計數器的關鍵。
function getUA() {
var ua = navigator.userAgent;
return ua.length > 250 ? ua.substring(0, 250) : ua;
}
function getBrower() {
var ua = getUA();
if (ua.indexOf("Maxthon") != -1) return "Maxthon";
if (ua.indexOf("MSIE") != -1) return "MSIE";
if (ua.indexOf("Firefox") != -1) return "Firefox";
if (ua.indexOf("Chrome") != -1) return "Chrome";
if (ua.indexOf("Opera") != -1) return "Opera";
if (ua.indexOf("Safari") != -1) return "Safari";
return "ot";
}
function getBrowerLanguage() {
var lang = navigator.browserLanguage;
return lang != null && lang.length > 0 ? lang : "";
}
function getPlatform() { return navigator.platform; }
function getPageTitle() { return document.title; }
UA 字串先截到 250 字元避免過長;瀏覽器判斷則是依序比對關鍵字。這裡有個細節值得注意:Chrome 的 UA 也含有 Safari 字樣,因此 Chrome 的判斷必須排在 Safari 之前,否則會被誤分類。
function vlstatInitLE(vlch, vlch1, vlch2, vlch3) {
var vlstatCookieId = getStatId();
var vlstatUA = encodeURIComponent(getUA());
var vlstatREFURL = encodeURIComponent(document.referrer);
var vlstatURL = encodeURIComponent(document.URL);
var vlstatScreenX = screen.width;
var vlstatScreenY = screen.height;
var p = "cookieId=" + vlstatCookieId
+ "&ua=" + vlstatUA
+ "&refurl=" + vlstatREFURL
+ "&url=" + vlstatURL
+ "&screenX=" + vlstatScreenX
+ "&screenY=" + vlstatScreenY
+ "&os=" + getPlatform()
+ "&brower=" + getBrower()
+ "&browerLang=" + getBrowerLanguage()
+ "&title=" + encodeURIComponent(getPageTitle());
createXMLHttpRequest();
xmlHttp.open('GET', 'index.php?' + p);
xmlHttp.send(null);
}
這段也示範了手寫 XMLHttpRequest 的相容寫法,同時照顧 ActiveXObject 與標準 XMLHttpRequest 兩種環境:
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
} else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
這支腳本寫法清楚、各函式職責單一,作為入門範例很合適。但放到今日來看有兩處需要調整:一是 document.localName 並不能取得真正的 IP,IP 應由伺服器端從請求標頭取得;二是 navigator.browserLanguage 已屬過時 API,建議改用 navigator.language 或 navigator.languages。此外 escape / unescape 也已不建議使用,統一改用 encodeURIComponent 即可。