A rchive Date
[ 10-06-2000 ]
Category
[ Information Technologies ]
sub-Categoy
[ Networking ]
|
[COOKIE RETRIEVAL
5.1 Introduction
For the most part, retrieving cookies does not require reading the HTTP Cookie: header. Most languages read this header for you and make it accessible through a variable or object.
Cookies can be read on the browser side or the server side. Again, the determining factor is the language used.
5.2 Retrieving cookies with JavaScript
To retrieve cookies with JavaScript, use document.cookie again. Typically, document.cookie has a string like so:
foo=bar;this=that;somename=somevalue;.....
This string contains every name-value pair valid for this document, separated by semicolons. This can make searching for your needed value a bit of a pain. The getCookie() function does make this simpler:
function getCookie(name) {
var cookie = " " + document.cookie;
var search = " " + name + "=";
var setStr = null;
var offset = 0;
var end = 0;
if (cookie.length > 0) {
offset = cookie.indexOf(search);
if (offset != -1) {
offset += search.length;
end = cookie.indexOf(";", offset)
if (end == -1) {
end = cookie.length;
}
setStr = unescape(cookie.substring(offset, end));
}
}
return(setStr);
}
The results are directed into variables in your routine:
myVar = GetCookie("foo");
Here, myVar would equal bar]
|