Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms

Making cookies - code


<head>

<script language="javascript">

function addHit(){

/*
Call readCookie and pass it counter and put the returned value into hits
*/
var hits = readCookie("counter")
if(hits == null){//The cookie does not exist
/*
Make a cookie with the name counter with a value of
0 that will expire in one year
*/
makeCookie("counter",0,365) /*
Show an alert stating that it's the first time the
user has come to this page
*/
alert("This is the first time you have been to this page")
}else{ //A cookie with the name exists
hits = parseInt(hits) + 1//Increase hits by one /*
Call make cookie and pass it the same name
and the value it was plus one
*/
makeCookie("counter",hits,365)
alert("You have visted this page " + (hits) + " times")//Show the alert
}
}

function readCookie(name){

/*
Get all the cookies associated with this document
*/
var c = document.cookie
var start = c.indexOf(name + "=")//Search for the name of the cookie
if(start == -1) return null//No cookie with that name found , terminate this process
/*
Move the start position to the end of the name and the equal sign(=)
*/
start+= name.length+1 /*
Search for the end position starting at start index
*/
var end = c.indexOf(";",start)
if(end != -1){//End was found
v = c.substring(start,end)//Set v to the string between start and end
}else{ //No end found
var v = c.substring(start)//Set v to the everything after start
}
return v //Return the value
}

function makeCookie(name,value,days,path,domain,secure){

if(name == null || value == null){//If name or value is nothing
return// terminate this process
}
var expires = "" //By default expires is not set
if(days){//If days is not null
var date = new Date()//Create a new date
var toDay = date.getTime()//Get todays date /*
Add todays date to the number of days this cookie should terminate
*/
expires = toDay + (days * 24*60*60*1000)
date.setTime(expires)//Use the setTime method and pass it expire date
expires = date.toUTCString()//Get the correct cookie date represenation /*
Format the expire's name/value pair
*/
expires = ";expires=" + expires
} /*
Format the path's name/value pair
*/
path = (path != null) ? ";path="+path : ";path=/" /*
Format domain's name/value pair
*/
domain = (domain != null) ? ";domain="+domain : "" /*
Format secure' name/value pair
*/
secure = (secure != null) ? ";secure="+secure : "" /*
Append this new cookie to the document.cookie property
*/
document.cookie = name + "=" + value +";" + expires + path + domain + secure
}

function deleteCookie(name,path,domain){

/*
Call the save cookie function and pass it -1 for the days parameter
*/
makeCookie(name,"",-1,path,domain)
}
</script>
</head>

<!--

Make the onLoad event of the document call the addHit function
--> <body
onLoad="addHit()"
>
<form>
<!--
Make the Delete the cookie file button call the
deleteCookie function and pass the cookie counter as the argument
-->
<input type="button" value="Delete the cookie file"
onclick="deleteCookie('counter')"> <!--
Make a convenience button to reload the document
(This will increase the number of hits by one)
-->
<input type="button" value="Reload"
onclick="window.location.reload()">
</form>
</body><!-- Close the body element -->


Close