stavros-s
3/30/2013 - 12:44 AM

JavaScript: Cookie-handling script.

JavaScript: Cookie-handling script.

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”

“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml”>


<head>

<title>Cookie Demo</title>



<meta
	http-equiv=”Content-Type”
	content=”text/html;
	charset=ISO-8859-1”
/>


<script
  type=”text/javascript”
  language=”javascript”
>

	now = new Date;

	expireDate = new Date;

	expireDate.setMonth
	(expireDate.getMonth()+6);

	hitCt = parseInt(cookieVal(“pageHit”));

	hitCt++;

	lastVisit = cookieVal(“pageVisit”);


	if (lastVisit == 0) 
	{
		lastVisit = “”
	}


	document.cookie 
	= “pageHit=”
	+hitCt
	+”;expires=”
	+ expireDate.toGMTString();


	document.cookie = “pageVisit=”+now+”;expires=” + expireDate.toGMTString()

	function cookieVal(cookieName)
	{

		thisCookie = document.cookie.split(“; “);

		for (i=0; i<thisCookie.length; i++) 
		{

			if (cookieName == thisCookie[i].split(“=”)[0])
			{
			  return thisCookie[i].split(“=”)[1];
			}

		}

	return 0
	}


</script>


</head>


<body>


<h2>


<script
	type=”text/javascript”
	language=”javascript”
>


	document.write
	(“You have visited this page “
	 + hitCt
	 + “ times.”);


	if (lastVisit != “”)
	{
		document.write
		(“<br />Your last visit was “ + lastVisit)
	}


</script>


</h2>


</body>


</html>