<!-- JavaScript:Cookies code snippet 3. -->
<!-- A universal cookie creating function with 3 cookie arguments.
Giving the ability to set name, value and expiry date. -->

<!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" xml:lang="en" lang="en">
<!-- Code source: http://www.justfigures.co.uk/ -->
<!-- A resource for web developers using XHTML, CSS, JavaScript, PHP -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>View code snippet page</title>
<script language="JavaScript" type="text/javascript">
//<![CDATA[
/* function getCookie() to retrieve the contents of a cookie 
by its name is detailed in JavaScript: Cookies code snippet 4 
and is therefore NOT included here to help clarity */
//]]>
</script>

<script language="JavaScript" type="text/javascript">
//<![CDATA[
// a universal cookie creating function with 3 cookie arguments
function setCookie(name,value,expires){
document.cookie=name+"="+value+";path=/"+((expires==null)?"":";expires="+expires.toGMTString())
}
//]]>
</script>
</head>

<body>
<script language="JavaScript" type="text/javascript">
//<![CDATA[
// create a value for the cookie
var cookieData=escape("name:eric penguin")
// create a new Date object
var expiryDate=new Date()
// set the expiry date of the cookie to 100 days in the future: a persistent cookie
expiryDate.setTime(expiryDate.getTime() + (1000*60*60*24*100))
// call the setCookie() function to create the cookie
setCookie('testCookie3',cookieData,expiryDate)

// write out the value from the cookie
document.write("Write out the value from the cookie with function getCookie()." + "<br />")
document.write("For function getCookie() see JavaScript: Cookies code snippet 4." + "<br />")
document.write("The cookie value is: " + getCookie("testCookie3"))
//]]>
</script>
</body>
</html>