<!-- JavaScript:Cookies code snippet 4. -->
<!-- A function to retrieve the contents of a cookie by its name. -->

<!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
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>
</head>

<body>
<script language="JavaScript" type="text/javascript">
//<![CDATA[
// create a value for the cookie
var cookieData = "eric"
// create cookie with document.cookie
document.cookie = "testCookie4" + "=" + escape(cookieData) + ";path=/"

// call the getCookie() function to write out the value from the cookie
document.write("The cookie value is: " + getCookie("testCookie4"))
//]]>
</script>
</body>
</html>