Use JavaScript to record the number of site visits

Source: Internet
Author: User
Tags date return set cookie setcookie split domain
javascript| Access We know that cookies are records of users who visit your site, reside on the user's hard disk, and if the user returns to your site again, the cookie will be sent back to your server to help you count and process the repeated visitors ' information. Support for COO Kie is provided in the IE4 and NS4 two browsers, and allows us to manipulate them in JavaScript or VBScript.

To get a simple idea of how cookies read and write, consider the following example.


Write cookies:
< script >
function Setcookie ()
Create a function that sets cookies
{var the_visits = 1;
Initialize visit, that is, the number of accesses is 1
var The_cookie = "3ware=uservisits:"
+ Escape (the_visits);
                   
Add your own identifier and encode the cookie with the escape function
Document.cookie = the_cookie;//Write Cookie
}
</script >

----This will generate a string 3ware=uservisits:1, which is to save a cookie named 3Ware to the hard disk, the value of the cookie is uservisits:1. When we read cookies, we simply look for a cookie with a name of 3Ware, then decode it with unescape and read out the number of accesses.
Read cookies:
< script >
function Readcookie ()
{var the_cookie = Document.cookie;
Get the written cookie value
var Broken_cookie = The_cookie.split (":");
Find the Separator
var the_visits = broken_cookie[1];
Read out the part after the separator
var the_visits = unescape (the_visits);
Decoding cookies with the Unescape function
Document.writeln ("Your Visits is:"
+ the_visits); Write out the number of visits
}
</script >

In this way, a string 3ware=uservisits:1 is generated that saves a cookie named 3Ware to the hard disk, and the value of the cookie is uservisits:1. When we read cookies, we simply look for a cookie with a name of 3Ware, then decode it with unescape and read out the number of accesses.


Read cookies:
< script >
function Readcookie ()
{var the_cookie = Document.cookie;
Get the written cookie value
var Broken_cookie = The_cookie.split (":");
Find the Separator
var the_visits = broken_cookie[1];
Read out the part after the separator
var the_visits = unescape (the_visits);
Decoding cookies with the Unescape function
Document.writeln ("Your Visits is:"
+ the_visits); Write out the number of visits
}
</script >

Finally, add "< script >setcookie (), Readcookie ();</script >" To remember the number of visits.

However, this is just the basic principle, the actual operation also involves the path of the cookie store, when the expiration, its effective scope is there, and check the number of visits exist and so on. Taking into account the above issues, the actual procedure is as follows:


< HTML >
< script language= "JavaScript" >
function Gookieval (offset)
Get the value of the cookie after decoding
{
var endstr = Document.cookie.
IndexOf (";", offset);
if (endstr = = 1)
Endstr = Document.cookie.length;
Return unescape (Document.cookie.
SUBSTRING (offset, endstr));
}
function Gookie (name)
Get the original value of the cookie
{var arg = name + ' = ';
var alen = Arg.length;
var clen = document.cookie.length;
var i = 0;
while (I < Clen)
{
var j = i + Alen;
if (Document.cookie.
SUBSTRING (i, j) = = arg)
Return Gookieval (j);
i = Document.cookie.indexOf ("", I) + 1;
if (i = = 0) break;
}
return null;
}
function Sookie (name, value)
Set cookie Value
{var argv = sookie.arguments;
var argc = Sookie.arguments.length;
var expires = (argc > 2)? ARGV[2]: null;
var path = (argc > 3)? ARGV[3]: null;
var domain = (argc > 4)? ARGV[4]: null;
var secure = (argc > 5)? ARGV[5]: false;
Document.cookie = name + "=" +
Escape (value) + ((expires = null)?
"" : ("; Expires= "+ expires.togmtstring ()))
+ ((path = null)? "" :
("; Path= "+ path") + ((domain = null)? ""
: ("; domain= "+ domain)"
+ (Secure = = True)? "; Secure ":" ");
}
function Dookie (name)
{var exp = new Date ();
Exp.settime (Exp.gettime ()-1);
var cval = Gookie (name);
Document.cookie = name + "=" + Cval + ";
Expires= "+ exp.togmtstring ();
}
</script >
< script >
var expdate = new Date ();
Expdate.settime (Expdate.gettime () +
(24 * 60 * 60 * 1000 * 365));
if (!) ( visits = Gookie ("3wave"))
{visits = 1;
Sookie ("3wave", visits, Expdate, "/", null, FALSE);
document.write ("This is your first" + visits + "visit this site");}
else{visits++;
Sookie ("3wave", visits, Expdate, "/", null, FALSE);
document.write ("You have the first" + Visits + "time to the station");
}
if (visits< 2)
document.write (", is a small station novice on the road, refueling Oh." ")
if (visits >=2&&visits< 4)
document.write (", is a small station of the junior stop friend, good." ")
if (visits >=4&&visits< 6)
document.write (", is the station's senior friend, very powerful Ah!" ")
if (visits >=6)
document.write (", is the pillar of the station, I was so touched!" ")
</script >

In the judgment of the number of visits expressed in the sentence, you can according to the style of the site, to have their own personality words, believe that if the visitors on your site to see these words, left him the impression must be very profound, so that you can make your site more attractive to others.

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.