curiouscat.comCurious Cat Code
curiouscat.com > code > JavaScript > Cookies > Count
Purpose: To count how many times a "visitor" has been to the site previously
Code: Using JavaScript and Cookies

Since we are using a cookie it will actually count the number of times the user has used that computer to visit the site since the cookie was created (or since it was last reset). For more on the use of cookies see Curious Cat Cookie Connections.

First we check to see if the cookie named counter already exists for this visitor:

var visits = getCookie("counter")
if (!visits)
visits = 1

If it already exists we add one to the current visit count and set the cookie to expire in 180 days.

else
visits = parseInt(visits) + 1
var expdate = new Date ();
expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 * 180));
setCookie("counter", visits, expdate);

Then print a message on the screen - such as we printed on the top of the Curious Cat Code - Cookie Counter JavaScript Example.
   

if (visits == 1) document.write("Thank you for what appears to be your first visit.") else document.write("Thank you for returning for what appears to be your visit number " + visits + "." )
View all the cut and paste code



© 1996-2008   curiouscat.com      Feedback - Contact Us
Curious Cat web site by Curious Cat Creations. We can help create or improve your online presence.