dcsimg

All explanation appears below in bold text.


<SCRIPT LANGUAGE="JavaScript">

<!-- Hide script

//This starts the script then hides it from lesser browsers.

cookie_name = "Counter_Cookie";

//The cookie name is set as Counter_Cookie. If you're going to put this onto other pages to count them, change this with a new name.

function doCookie() {
if(document.cookie)
{index = document.cookie.indexOf(cookie_name);}

//Is there a cookie named Counter_Cookie? If so, use that.

else
{index = -1;}

//If there isn't, set the index to minus one (-1).

var expires = "Monday, 04-Apr-2010 05:00:00 GMT"

//A variable is set up to represent the expires date.

if (index == -1)
{document.cookie=cookie_name+"=1; expires=" + expires;}

//If the index was set to minus one, then set the cookie with the name Counter_Cookie, a number 1, then the expires date.

else
{
countbegin = (document.cookie.indexOf("=", index) + 1);
countend = document.cookie.indexOf(";", index);
if (countend == -1) {
countend = document.cookie.length;
}
count = eval(document.cookie.substring(countbegin, countend)) + 1;

document.cookie=cookie_name+"="+count+"; expires=" + expires;
}
}

//If not, then take the number in the cookie already and add one. Then rewrite the cookie with the new number.


function gettimes() {

//This starts the second function that gets the value in the cookie and assigns it to a variable name.

if(document.cookie) {
index = document.cookie.indexOf(cookie_name);
if (index != -1) {
countbegin = (document.cookie.indexOf("=", index) + 1);
countend = document.cookie.indexOf(";", index);

//Grab the second piece of information in the cookie, the number (see the +1?).

if (countend == -1) {
countend = document.cookie.length;
}
count = document.cookie.substring(countbegin, countend);
if (count == 1) {
return (count+" time");
} else {
return (count+" times");

//If the count is one, then return the singular "time." If the count is more, return the plural "times."

}
}
}
return ("0 times");

//If the number is 0, then return "0 times."

}

// done hiding script -->

</SCRIPT>

//End the script.


<body onLoad="doCookie()">

//An onLoad Event Handler fires the function to place the cookie.

<center>
<SCRIPT LANGUAGE="javascript">
document.write("<b>You have been to my site "+gettimes()+" before.</b>");
</SCRIPT>

</center>

//Another script uses a document.write statement to place the output of the gettimes() function on the page.