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:


Here's the function, which was written by Shelley Powers and have long been released to the public freely:

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;

1) Set a session only cookie using the syntax:


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.