javascript实现的网站访问量统计代码
网站一般都有访问量统计工具,比较高效实用的工具多种多样,并且非常的精确。实用javascript也可以简单的实现此功能,尽管没有网络上常用的精确,不过的确也实现了一定的功能,下面就是一段代码实例,感兴趣的朋友可以参考一下:
废话不多说了,直接给大家贴js代码了。
<scripttype="text/javascript"> /** *vlstat浏览器统计脚本 */ varstatIdName="vlstatId"; varxmlHttp; /** *设置cookieId */ functionsetCookie(c_name,value,expiredays){ varexdate=newDate(); exdate.setDate(exdate.getDate()+expiredays); document.cookie=c_name+"="+escape(value)+((expiredays==null)?"":";expires="+exdate.toGMTString())+";path=/;domain=cecb2b.com"; } /** *获取cookieId */ functiongetCookie(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; } returnunescape(document.cookie.substring(c_start,c_end)); } } return""; } /** *获取当前时间戳 */ functiongetTimestamp(){ vartimestamp=Date.parse(newDate()); returntimestamp; } /** *生成statId */ functiongenStatId(){ varcookieId=getTimestamp(); cookieId="vlstat"+"-"+cookieId+"-"+Math.round(Math.random()*3000000000); returncookieId; } /** *设置StatId */ functionsetStatId(){ varcookieId=genStatId(); setCookie(statIdName,cookieId,365); } /** *获取StatId */ functiongetStatId(){ varstatId=getCookie(statIdName); if(statId!=null&&statId.length>0){ returnstatId; }else{ setStatId(); returngetStatId(); } } /** *获取UA */ functiongetUA(){ varua=navigator.userAgent; if(ua.length>250){ ua=ua.substring(0,250); } returnua; } /** *获取浏览器类型 */ functiongetBrower(){ varua=getUA(); if(ua.indexOf("Maxthon")!=-1){ return"Maxthon"; }elseif(ua.indexOf("MSIE")!=-1){ return"MSIE"; }elseif(ua.indexOf("Firefox")!=-1){ return"Firefox"; }elseif(ua.indexOf("Chrome")!=-1){ return"Chrome"; }elseif(ua.indexOf("Opera")!=-1){ return"Opera"; }elseif(ua.indexOf("Safari")!=-1){ return"Safari"; }else{ return"ot"; } } /** *获取浏览器语言 */ functiongetBrowerLanguage(){ varlang=navigator.browserLanguage; returnlang!=null&&lang.length>0?lang:""; } /** *获取操作系统 */ functiongetPlatform(){ returnnavigator.platform; } /** *获取页面title */ functiongetPageTitle(){ returndocument.title; } /** *创建一个form * *@return */ functioncreateSubmitForm(){ varfrm=document.createElement("form"); document.body.appendChild(frm); frm.method="POST"; returnfrm; } /** *为form创建一个元素 * *@paraminputForm *@paramelementName *@paramelementValue *@return */ functioncreateFormElement(frmInput,elementName,elementValue){ varelement=document.createElement("input"); element.setAttribute("id",elementName); element.setAttribute("name",elementName); element.setAttribute("type","hidden"); element.setAttribute("value",elementValue); frmInput.appendChild(element); returnelement; } /** *构造XMLHttpRequest对象 * *@return */ functioncreateXMLHttpRequest(){ if(window.ActiveXObject){ xmlHttp=newActiveXObject('Microsoft.XMLHTTP'); }elseif(window.XMLHttpRequest){ xmlHttp=newXMLHttpRequest(); } } /** *url指定跳转页,data是要post的数据。func类似于函数指针 * *@paramurl *@paramdata *@paramfunc *@return */ functionAjaxPost(url,data,func){ varhttpRequest=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("ErrorXMLHttpRequest!"); } } } }else{ alert("ErrorinitializingXMLHttpRequest!"); } } functionvlstatInitLE(vlch,vlch1,vlch2,vlch3){ varp; varvlstatCH=vlch!=null&&vlch.length>0?vlch:""; varvlstatCH1=vlch1!=null&&vlch1.length>0?vlch1:""; varvlstatCH2=vlch2!=null&&vlch2.length>0?vlch2:""; varvlstatCH3=vlch3!=null&&vlch3.length>0?vlch3:""; varvlstatCookieId=getStatId(); varvlstatUA=encodeURIComponent(getUA()); varvlstatIPAddress=document.localName; varvlstatREFURL=encodeURIComponent(document.referrer); varvlstatURL=encodeURIComponent(document.URL); varvlstatScreenX=screen.width; varvlstatScreenY=screen.height; varvlstatOS=getPlatform(); varvlstatBrower=getBrower(); varvlstatBrowerLanguage=getBrowerLanguage(); varvlstatPageTitle=encodeURIComponent(getPageTitle()); varvlstatAction="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; varurlGo=vlstatAction+"?"+p; createXMLHttpRequest(); xmlHttp.open('GET',urlGo); xmlHttp.send(null); } </script>
下面在给大家分享一段代码实例-JS精确统计网站访问量程序
/** *vlstat浏览器统计脚本 */ varstatIdName="vlstatId"; varxmlHttp; /** *设置cookieId */ functionsetCookie(c_name,value,expiredays){ varexdate=newDate(); exdate.setDate(exdate.getDate()+expiredays); document.cookie=c_name+"="+escape(value)+((expiredays==null)?"":";expires="+exdate.toGMTString())+";path=/;domain=cecb2b.com"; } /** *获取cookieId */ functiongetCookie(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; } returnunescape(document.cookie.substring(c_start,c_end)); } } return""; } /** *获取当前时间戳 */ functiongetTimestamp(){ vartimestamp=Date.parse(newDate()); returntimestamp; } /** *生成statId */ functiongenStatId(){ varcookieId=getTimestamp(); cookieId="vlstat"+"-"+cookieId+"-"+Math.round(Math.random()*3000000000); returncookieId; } /** *设置StatId */ functionsetStatId(){ varcookieId=genStatId(); setCookie(statIdName,cookieId,365); } /** *获取StatId */ functiongetStatId(){ varstatId=getCookie(statIdName); if(statId!=null&&statId.length>0){ returnstatId; }else{ setStatId(); returngetStatId(); } } /** *获取UA */ functiongetUA(){ varua=navigator.userAgent; if(ua.length>250){ ua=ua.substring(0,250); } returnua; } /** *获取浏览器类型 */ functiongetBrower(){ varua=getUA(); if(ua.indexOf("Maxthon")!=-1){ return"Maxthon"; }elseif(ua.indexOf("MSIE")!=-1){ return"MSIE"; }elseif(ua.indexOf("Firefox")!=-1){ return"Firefox"; }elseif(ua.indexOf("Chrome")!=-1){ return"Chrome"; }elseif(ua.indexOf("Opera")!=-1){ return"Opera"; }elseif(ua.indexOf("Safari")!=-1){ return"Safari"; }else{ return"ot"; } } /** *获取浏览器语言 */ functiongetBrowerLanguage(){ varlang=navigator.browserLanguage; returnlang!=null&&lang.length>0?lang:""; } /** *获取操作系统 */ functiongetPlatform(){ returnnavigator.platform; } /** *获取页面title */ functiongetPageTitle(){ returndocument.title; } /** *创建一个form * *@return */ functioncreateSubmitForm(){ varfrm=document.createElement("form"); document.body.appendChild(frm); frm.method="POST"; returnfrm; } /** *为form创建一个元素 * *@paraminputForm *@paramelementName *@paramelementValue *@return */ functioncreateFormElement(frmInput,elementName,elementValue){ varelement=document.createElement("input"); element.setAttribute("id",elementName); element.setAttribute("name",elementName); element.setAttribute("type","hidden"); element.setAttribute("value",elementValue); frmInput.appendChild(element); returnelement; } /** *构造XMLHttpRequest对象 * *@return */ functioncreateXMLHttpRequest(){ if(window.ActiveXObject){ xmlHttp=newActiveXObject('Microsoft.XMLHTTP'); }elseif(window.XMLHttpRequest){ xmlHttp=newXMLHttpRequest(); } } /** *url指定跳转页,data是要post的数据。func类似于函数指针 * *@paramurl *@paramdata *@paramfunc *@return */ functionAjaxPost(url,data,func){ varhttpRequest=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("ErrorXMLHttpRequest!"); } } } }else{ alert("ErrorinitializingXMLHttpRequest!"); } } functionvlstatInitLE(vlch,vlch1,vlch2,vlch3){ varp; varvlstatCH=vlch!=null&&vlch.length>0?vlch:""; varvlstatCH1=vlch1!=null&&vlch1.length>0?vlch1:""; varvlstatCH2=vlch2!=null&&vlch2.length>0?vlch2:""; varvlstatCH3=vlch3!=null&&vlch3.length>0?vlch3:""; varvlstatCookieId=getStatId(); varvlstatUA=encodeURIComponent(getUA()); varvlstatIPAddress=document.localName; varvlstatREFURL=encodeURIComponent(document.referrer); varvlstatURL=encodeURIComponent(document.URL); varvlstatScreenX=screen.width; varvlstatScreenY=screen.height; varvlstatOS=getPlatform(); varvlstatBrower=getBrower(); varvlstatBrowerLanguage=getBrowerLanguage(); varvlstatPageTitle=encodeURIComponent(getPageTitle()); varvlstatAction="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; varurlGo=vlstatAction+"?"+p; createXMLHttpRequest(); xmlHttp.open('GET',urlGo); xmlHttp.send(null); }