I must have used this function a hundred times now to retrieve a session only cookie, so I thought I'd post it here. It's very useful in painlessly retrieving the value of a cookie set using something like:
document.cookie="myname=George"
Here's the function, which was written by Shelley Powers and have long been released to the public freely:
Code:
function get_cookie(Name) {
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
// if cookie exists
if (offset != -1) {
offset += search.length
// set index of beginning of value
end = document.cookie.indexOf(";", offset);
// set index of end of cookie value
if (end == -1) end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}
Usage:
1) Set a session only cookie using the syntax:
document.cookie="myname=George"
where "George" is the value of a cookie called "myname"
2) Then retreive "George, call the above function like so:
var result=get_cookie("myname")
Should the corresponding value not exist, the function returns an empty string.