Javascript Browser Cookies - Retrieving a Cookie
|
Get a Cookie - Javascript
The javascript cookie methods on these pages demonstrate how to:- set/put a cookie - Try it
- get/read a cookie - Try it
- delete/remove a cookie - Try it
- restrict webpage access based on a cookie - Try it
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;
}
}
}
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;
}
}
}
Men's Houston Texans '47 Brand Navy Turner Clean Up Adjustable Hat,Men's Pittsburgh Steelers G-III Sports by Carl Banks Charcoal/Black Victor Reversible Wool Jacket.Men's Pro Line New England Patriots Steve Grogan Retired Player Jersey,Pittsburgh Steelers Swipe Bluetooth Headphones,Baltimore Ravens Women's Three-Quarter Sleeve Raglan Tri-Blend T-Shirt - Ash/Purple.Nike Baltimore Ravens Fast Wordmark T-Shirt - Purple,Visit ESPN to get up-to-the-minute sports news coverage, scores, highlights and commentary for NFL, MLB, NBA, College Football, NCAA Basketball and more Oakland Raiders Jerseys for Sale..Men's Philadelphia Eagles G-III Sports by Carl Banks Charcoal Franchise Half-Zip Jacket,Preschool Chicago Bears For Bare Feet Big Logo & Name Socks.New England Patriots Infant T-Shirt & Short Set - Navy Blue,jerseyshop.cn: All you need to know to buy jerseys online Future Of The Oakland Raiders.Men's Pittsburgh Steelers Antonio Brown Nike Player Pride Name & Number T-Shirt - Black,San Francisco 49ers Medium Locker Room Duffle Bag - Scarlet Oakland Raiders Jerseys Cheap.Men's San Francisco 49ers Cutter & Buck Scarlet Broadview V-Neck Sweater,Minnesota Vikings Mural Love Backpack Oakland Raider Jerseys Sale.Men's Chicago Bears New Era Navy Super Bowl XX On The Fifty Jumbo Vize Original Fit 9FIFTY Adjustable Hat,Women's Oakland Raiders Majestic Charcoal Plus Size Crossblock Pullover Sweatshirt,Denver Broncos Small Window Cling
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.
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: