JavaScript實現網站訪問次數統計代碼_javascript技巧

來源:互聯網
上載者:User

作為一個網站的管理員或者說站長,都希望知道到底有多少人訪問了網站,這個時候就需要有一個統計功能來滿足需要,當然功能比較單一和簡單,如果想要強大的統計效果,那最好還是使用現在比較成熟的統計工具,比如站長統計或者騰訊統計等等。

代碼一:

代碼如下:

<script type="text/javascript">var caution=falsefunction setCookie(name,value,expires,path,domain,secure) { var curCookie=name+"="+escape(value) + ((expires)?";expires="+expires.toGMTString() : "") + ((path)?"; path=" + path : "") + ((domain)? "; domain=" + domain : "") + ((secure)?";secure" : "") if(!caution||(name + "=" + escape(value)).length <= 4000) { document.cookie = curCookie } else if(confirm("Cookie exceeds 4KB and will be cut!")) { document.cookie = curCookie }}function getCookie(name) { var prefix = name + "=" var cookieStartIndex = document.cookie.indexOf(prefix) if (cookieStartIndex == -1) { return null }     var cookieEndIndex=document.cookie.indexOf(";",cookieStartIndex+prefix.length) if(cookieEndIndex == -1) { cookieEndIndex = document.cookie.length } return unescape(document.cookie.substring(cookieStartIndex+prefix.length,cookieEndIndex))}function deleteCookie(name, path, domain) { if(getCookie(name))  { document.cookie = name + "=" +  ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT" }}function fixDate(date) { var base=new Date(0) var skew=base.getTime() if(skew>0) { date.setTime(date.getTime()-skew) }    }var now=new Date()fixDate(now)now.setTime(now.getTime()+365 * 24 * 60 * 60 * 1000)var visits = getCookie("counter")if(!visits){ visits=1;}  else{ visits=parseInt(visits)+1;}  setCookie("counter", visits, now)document.write("您是到訪的第" + visits + "位使用者!")</script>

代碼二:

代碼如下:

<script type="text/javascript">var caution=falsefunction setCookie(name,value,expires,path,domain,secure) { var curCookie=name+"="+escape(value) + ((expires)?";expires="+expires.toGMTString() : "") + ((path)?"; path=" + path : "") + ((domain)? "; domain=" + domain : "") + ((secure)?";secure" : "") if(!caution||(name + "=" + escape(value)).length <= 4000) { document.cookie = curCookie } else if(confirm("Cookie exceeds 4KB and will be cut!")) { document.cookie = curCookie }}function getCookie(name) { var prefix = name + "=" var cookieStartIndex = document.cookie.indexOf(prefix) if (cookieStartIndex == -1) { return null }     var cookieEndIndex=document.cookie.indexOf(";",cookieStartIndex+prefix.length) if(cookieEndIndex == -1) { cookieEndIndex = document.cookie.length } return unescape(document.cookie.substring(cookieStartIndex+prefix.length,cookieEndIndex))}function deleteCookie(name, path, domain) { if(getCookie(name))  { document.cookie = name + "=" +  ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT" }}function fixDate(date) { var base=new Date(0) var skew=base.getTime() if(skew>0) { date.setTime(date.getTime()-skew) }    }var now=new Date()fixDate(now)now.setTime(now.getTime()+365 * 24 * 60 * 60 * 1000)var visits = getCookie("counter")if(!visits){ visits=1;}  else{ visits=parseInt(visits)+1;}  setCookie("counter", visits, now)document.write("您是到訪的第" + visits + "位使用者!")</script>

以上通過兩種方式實現JavaScript統計網站訪問量的代碼,希望對大家有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.