Written by davidonzo on 24/04/2008, Filled in OpenSource, Web, Tutorial

A simple function to read a cookie value.

 

function CookieValue (offset) {
  var stringend = document.cookie.indexOf (";", offset);
  if (stringend == -1)
   stringend = document.cookie.length;
  return unescape(document.cookie.substring(offset, stringend)).replace("+"," ");
}

  function GetCookie (name) {
   var argu = name + "=";
   var alen = arg.length;
   var clen = document.cookie.length;
   var none = '';
   var i = 0;
   while (i < clen) {
    var a = i + alen;
      if (document.cookie.substring(i, a) == arg)
        return CookieValue(a);
      i = document.cookie.indexOf(" ", i) + 1;
      if (i == 0) break;
  }
 return none;
}

 

How to use it?

An example may be the best way to understand hot to use this code. Assum you have a cookie named "MyCookie" with value "MyCookie+Value".

The function:

 

GetCookie('MyCookie')

 

Return: "MyCookie Value".

 

I'm working hard in this period. I hope to have more time to update the blog ASAP :)

Did you find interesting this article?
Subscribe my feed to be advised of any new post!
 
.Comments rss

No comments yet...



Comments could be moderated.
If you don't see it immediately published, please avoid to insert it one more time.
Thanks for your patient.