<!-- JavaScript:Cookies code snippet 10. -->
<!-- Retrieve the contents of a cookie identified by its name.
Use 'split()' to retrieve and separate the descriptor and value. -->
<!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[
// a universal cookie creating function with 3 cookie arguments
// function setCookie() was introduced in JavaScript: Cookies code snippet 3
function setCookie(name,value,expires){
document.cookie = name + "=" + value + ";path=/" + ((expires==null) ? "" : ";expires=" + expires.toGMTString())
}
//]]>
</script>
<script language="JavaScript" type="text/javascript">
//<![CDATA[
// function to retrieve the contents of a cookie by its name
// function getCookie() was introduced in JavaScript: Cookies code snippet 4
function getCookie(name)
{
// concatenate '=' to the cookie name
var cookieName = name + "="
// assign document.cookie to a variable
var docCookie = document.cookie
var cookieStart
var end
if (docCookie.length>0)
{
/* search for the location of the named cookie
in the document.cookie object string which may
contain more than one cookie separated by '; ' */
cookieStart = docCookie.indexOf(cookieName)
// if the cookie does not exist then return false
if (cookieStart != -1)
{
// if the cookie exists then go to the end of the cookie name
cookieStart = cookieStart + cookieName.length
/* look for the ';' that defines the end of the named
cookie we need, beginning at cookieStart */
end = docCookie.indexOf(";",cookieStart)
if (end == -1)
{
end = docCookie.length
}
// finally extract the required string
return unescape(docCookie.substring(cookieStart,end))
}
}
return false
}
//]]>
</script>
<script language="JavaScript" type="text/javascript">
//<![CDATA[
// function to split the cookie descriptor and value
function breakCookie(content){
// split the descriptor and value into an array
var separated_values = content.split(":")
// assign the values of the array to variables and write to the page
var the_descriptor = (separated_values[0])
document.write("The descriptor is: "+the_descriptor+"<br />")
var the_value = (separated_values[1])
document.write("The value is: "+the_value+"<br />")
}
//]]>
</script>
</head>
<body>
<script language="JavaScript" type="text/javascript">
//<![CDATA[
// create values for the cookie
var cookieData = escape("name:eric")
// create a new Date object
var expiryDate=new Date()
// set the expiry date of the cookie to 100 days in the future
expiryDate.setTime(expiryDate.getTime() + (1000*60*60*24*100))
// call the setCookie() function to create the cookie
setCookie('testCookie10',cookieData,expiryDate)
// assign the descriptor and value from the cookie to a variable
var allCookieContent=getCookie("testCookie10")
// write out the descriptor and value from the cookie
document.write("The cookie descriptor and value is: " + allCookieContent + "<br />")
// call the function that separates the descriptor and value
breakCookie(allCookieContent)
//]]>
</script>
</body>
</html>