javascript cookies
Get a Cookie - Javascript
The javascript cookie methods on these pages demonstrate how to:- set/put a cookie
- get/read a cookie
- delete/remove a cookie
- restrict webpage access based on a cookie
Retrieve the Cookie
This sample page demonstrates how to retrieve a client-side cookie from the visitor's browser using Javascript. This is the cookie that we set on the Setting a Javascript Cookie page.
Retrieve a Javascript Cookie
Your cookie currently holds this value:
The following sample code demonstrates one method for getting the previously stored
cookie. Implement the following javascript function call on your HTML page at the point
in the document flow that you wish to
display the stored cookie value:
Your cookie currently holds this value:
<script type="text/javascript">cookieGet();</script>
The cookieGet() javascript function needs to be defined, either before the ending
<head> tag in the current document or in an external javascript file, as follows:
var cookieText = "Put your desired cookie value here";
var cookiePrefix = "";
var myPage = location.href;
var wwwFlag = myPage.indexOf('www');
if (wwwFlag > 0) {
cookiePrefix = "www";
}
var cookieName = cookiePrefix + "cbCookie";
var YouWrote;
function cookieGet() {
if (document.cookie) {
index = document.cookie.indexOf(cookie_name);
if (index != -1) {
namestart = (document.cookie.indexOf("=", index) + 1);
nameend = document.cookie.indexOf("cbEndCookie;", index);
if (nameend == -1) {
nameend = 0;
}
YouWrote = document.cookie.substring(namestart, nameend);
return YouWrote;
}
}
}
Notice that our cookie-related variables are declared outside (before) the cookieGet()
function. This ensures that the cookieName will be available from elsewhere on
our HTML page, which will be helpful if we wish to read, or get, the cookie again elsewhere
on the page. The wwwFlag
checks to see if there is a 'www' in the domain name, and attaches the
prefix accordingly. Without this differentiation, trouble can arise when we attempt to read the
cookie, since a unique cookie is set depending on whether or not the 'www' is present in the
domain name.
var cookiePrefix = "";
var myPage = location.href;
var wwwFlag = myPage.indexOf('www');
if (wwwFlag > 0) {
cookiePrefix = "www";
}
var cookieName = cookiePrefix + "cbCookie";
var YouWrote;
function cookieGet() {
if (document.cookie) {
index = document.cookie.indexOf(cookie_name);
if (index != -1) {
namestart = (document.cookie.indexOf("=", index) + 1);
nameend = document.cookie.indexOf("cbEndCookie;", index);
if (nameend == -1) {
nameend = 0;
}
YouWrote = document.cookie.substring(namestart, nameend);
return YouWrote;
}
}
}
About the Cookie
The cookie is a tiny, harmless (dare I say friendly?) little guy - just a plain text file. Mozilla browsers will aggregate cookies from all websites into one file called, appropriately enough, cookies.txt. Within Internet Explorer, each unique website that puts cookies on your machine has a separate cookie file. For Windows XP, the default cookie locations are indicated below:Netscape:
C:\Documents and Settings\[XPuser]\Application Data\Mozilla\Profiles\[your_profile]\cookies.txt
Firefox:
C:\Documents and Settings\[XPuser]\Application Data\Mozilla\Firefox\Profiles\[your_profile]\cookies.txt
IE: (two locations)
1. C:\Documents and Settings\[XPuser]\Cookies
2. C:\Documents and Settings\[XPuser]\Local Settings\Temporary Internet Files\cookie_name.txt
In IE, the cookie names appear differently, depending on the directory that you're viewing. The names also vary depending on whether the cookie is set by a page in the root directory of the website, or in a subdirectory.
1. In the first IE cookie location:
A cookie set by a page in the root directory has a filename in the format:
'[XPuser]@domainname.txt', with the domain name extension omitted.
A cookie set by a page residing in a subdirectory has a filename in the format:
'[XPuser]@[directory_name].txt.
2. In the second IE cookie location:
Cookie set from root has filename 'Cookie:[XPuser]@domainname/'.
Cookie set from subdirectory has filename '[directory_name]'.
Related Pages: [the end]
The method(s) on this page have been tested on Windows XP, using
Internet Explorer 6.0, Firefox 1.5, and Netscape 7.2.
Popup Windows
This page covers the basics of popup windows - opening, closing, and changing URLs.
Javascript Cookies
Learn the basics of manipulating cookies with javascript - setting, getting, deleting.
VoiceXML Demo
A simple, toll-free demonstration of telephone and web application interaction.
©2007 chris brenz, brenz.net

