怎樣在網站中實現統計訪問量的功能
阿新 • • 發佈:2017-07-20
time() width color str doc als 效果 conf 保留
有很多網站都會加一個訪問量統計的功能,其實代碼很簡單,全都是js寫的,而且代碼也不多
這個功能解釋一下就是,每訪問一次,網頁上面寫的訪問量就會增加一個,依次類推,下面就與大家分享一下實現這個功能的一段代碼
<script language=JavaScript> <!-- var caution = false function 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>
只需要這一段代碼就可以了,把它放在你想要放的位置,網站裏就會有這個功能了,看一下實現的效果圖
從我放上這個功能後,一共訪問了3次,所以上面顯示到了第3位,感興趣的也來試一下吧,喜歡的可以自己保留
怎樣在網站中實現統計訪問量的功能