This article introduces the sample code for JS to accurately count website visits. If you need it, refer to it.
The Code is as follows:
/**
* Vlstat browser statistics script
*/
Var statIdName = "vlstatId ";
Var xmlHttp;
/**
* Set 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 ";
}
/**
* Obtain the 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 "";
}
/**
* Get the current Timestamp
*/
Function getTimestamp (){
Var timestamp = Date. parse (new Date ());
Return timestamp;
}
/**
* Generate statId
*/
Function genStatId (){
Var cookieId = getTimestamp ();
CookieId = "vlstat" + "-" + cookieId + "-" + Math. round (Math. random () * 3000000000 );
Return cookieId;
}
/**
* Set StatId
*/
Function setStatId (){
Var cookieId = genStatId ();
SetCookie (statIdName, cookieId, 365 );
}
/**
* Get StatId
*/
Function getStatId (){
Var statId = getCookie (statIdName );
If (statId! = Null & statId. length> 0 ){
Return statId;
} Else {
SetStatId ();
Return getStatId ();
}
}
/**
* Obtain UA
*/
Function getUA (){
Var ua = navigator. userAgent;
'If (ua. length> 250 ){
Ua = ua. substring (0,250 );
}
Return ua;
}
/**
* Obtain the browser type
*/
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 ";
}
}
/**
* Obtain the browser Language
*/
Function getBrowerLanguage (){
Var lang = navigator. browserLanguage;
Return lang! = Null & lang. length> 0? Lang :"";
}
/**
* Obtain the Operating System
*/
Function getPlatform (){
Return navigator. platform;
}
/**
* Obtain the page title
*/
Function getPageTitle (){
Return document. title;
}
/**
* Create a form
*
* @ Return
*/
Function createSubmitForm (){
Var frm = document. createElement ("form ");
Document. body. appendChild (frm );
Frm. method = "POST ";
Return frm;
}
/**
* Create an element for 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;
}
/**
* Construct an XMLHttpRequest object
*
* @ Return
*/
Function createXMLHttpRequest (){
If (window. ActiveXObject ){
XmlHttp = new ActiveXObject ('Microsoft. xmlhttp ');
} Else if (window. XMLHttpRequest ){
XmlHttp = new XMLHttpRequest ();
}
}
/**
* The url specifies the jump page. data is the data to be post. Func is similar to function pointer.
*
* @ 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 ){
// You can call the desired function here
Func (httpRequest. responseText );
}
}
} Catch (e ){
Alert ("Error XMLHttpRequest! ");
}
}
}
} Else {
Alert ("Error initializing XMLHttpRequest! ");
}
}
Function vlstatInitLE (vlch, vlch, vlch2, vlch3 ){
Var p;
Var vlstatCH = vlch! = Null & vlch. length> 0? Vlch :"";
Var vlstatpattern = vldid! = Null & vlch1.length> 0? Vldid :"";
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 + "& Signature =" + vlstatCH + "& ch2 =" + vlstatCH2
+ "& Ch3 =" + vlstatCH3;
Var urlGo = vlstatAction + "? "+ P;
CreateXMLHttpRequest ();
XmlHttp. open ('get', urlGo );
XmlHttp. send (null );
}