<!-- JavaScript:Cookies code snippet 8. -->
<!-- A universal cookie creating function with 6 cookie arguments.
Giving the ability to set the cookie name, value, expiry date,
path, domain and security. -->
<!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 to retrieve the contents of a cookie by its name
see Javascript: Cookies code snippet 4 for details */
function getCookie(name)
{
var cookieName = name + "="
var docCookie = document.cookie
var cookieStart
var end
if (docCookie.length>0)
{
cookieStart = docCookie.indexOf(cookieName)
if (cookieStart != -1)
{
cookieStart = cookieStart + cookieName.length
end = docCookie.indexOf(";",cookieStart)
if (end == -1)
{
end = docCookie.length
}
return unescape(docCookie.substring(cookieStart,end))
}
}
return false
}
//]]>
</script>
<script language="JavaScript" type="text/javascript">
//<![CDATA[
/* a universal cookie creating function with 6 cookie arguments */
function setCookie(name,value,expires,path,domain,secure) {
var theCookie = name + "=" + value;
if (expires) {
theCookie += "; expires=" + expires.toGMTString();
}
if (path) {
theCookie += "; path=" + path;
}
if (domain) {
theCookie += "; domain=" + domain;
}
if (secure) {
theCookie += "; secure";
}
document.cookie = theCookie;
}
//]]>
</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 1 day in the future
expiryDate.setTime(expiryDate.getTime() + (1000*60*60*24*1))
/* call the setCookie() function to create a cookie,
note that for this cookie we have set the domain but not the security */
setCookie('testCookie8',cookieData,expiryDate,'/','justfigures.co.uk')
// write out the value from the cookie
document.write("Write out the value from the cookie where security is NOT set as requiring a secure https protocol site." + "<br />")
document.write("The cookie value is: " + getCookie("testCookie8")+ "<br />")
/* call the setCookie() function to create a cookie,
note that for this cookie we have set the domain and the secure https protocol site requirement */
setCookie('testCookie8s',cookieData,expiryDate,'/','justfigures.co.uk','secure')
// write out the value from the cookie
document.write("Write out the value from the cookie where security is set as requiring a secure https protocol site." + "<br />")
document.write("The secure cookie value is: " + getCookie("testCookie8s"))
//]]>
</script>
</body>
</html>