How to achieve the function of statistic traffic in the website

Source: Internet
Author: User
Tags setcookie

There are many sites will add a traffic statistics function, in fact, the code is very simple, all JS written, and the code is not much

This function explains that, every visit, the amount of traffic written on the page will be increased by one, and so on, the following to share with you a piece of code to implement this function

<script language=javascript><!--varCaution =falsefunctionSetcookie (name, value, expires, path, domain, secure) {varCurcookie = name + "=" + Escape (value) +((expires)? "; Expires= "+ expires.togmtstring ():" ") +(PATH)? "; Path= "+ Path:" ") +(domain)? "; domain= "+ domain:" ") +(Secure)? "; Secure ":" ")if(!caution | | (name + "=" + Escape (value)). Length <= 4000) Document.cookie=CurcookieElseif(Confirm ("Cookie exceeds 4KB and would be cut!")) Document.cookie=Curcookie}functionGetCookie (name) {varprefix = name + "="varCookiestartindex =document.cookie.indexOf (prefix)if(Cookiestartindex = =-1)return NULLvarCookieendindex = Document.cookie.indexOf (";", Cookiestartindex +prefix.length)if(Cookieendindex = =-1) Cookieendindex=Document.cookie.lengthreturnUnescape (document.cookie.substring (Cookiestartindex +prefix.length, Cookieendindex)}functionDeletecookie (name, path, domain) {if(GetCookie (name)) {Document.cookie= name + "=" +(PATH)? "; Path= "+ Path:" ") +(domain)? "; domain= "+ domain:" ") +"; Expires=thu, 01-jan-70 00:00:01 GMT "}}functionfixdate (date) {varBase =NewDate (0)varSkew =base.gettime ()if(Skew > 0) Date.settime (Date.gettime ()-skew)}varnow =NewDate () fixdate (now) Now.settime (Now.gettime ()+ 365 * 24 * 60 * 60 * 1000)varvisits = GetCookie ("Counter")if(!visits) Visits= 1Elsevisits= parseint (visits) + 1Setcookie ("Counter", visits, now) document.write ("You are the first" + Visits + "bit to access this topic! ")// -</script>

Just need this piece of code on it, put it where you want to put it, the site will have this function, look at the implementation of the

Since I put this function, a total of 3 visits, so the above is shown to the 3rd place, interested also to try it, like can keep their own

How to achieve the function of statistic traffic in the website

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.