javascript實現的網站訪問量統計代碼_javascript技巧

來源:互聯網
上載者:User

網站一般都有訪問量統計工具,比較高效實用的工具多種多樣,並且非常的精確。實用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); }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.