home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 September / Chip_2002-09_cd1.bin / tema / neural / webtest / CDSpace / Gemini / cookies.js < prev    next >
Text File  |  2000-08-05  |  2KB  |  60 lines

  1. function getCookie(NameOfCookie)
  2. {
  3.  
  4. // First we check if there is a cookie stored at all.
  5. // Otherwise the length of document.cookie would be zero.
  6.  
  7.   if (document.cookie.length > 0) 
  8.  
  9. // Second we check if the cookies name is stored in the
  10. // "document.cookie"-object for the page.
  11.  
  12. // Since more than just one cookie can be set on a
  13. // single page it is possible that our cookie
  14. // is not present, even though the "document.cookie"-object
  15. // is not just an empty text.
  16. // If our cookiename is not present the value -1 is stored
  17. // in the variable called "begin".
  18.  
  19.     begin = document.cookie.indexOf(NameOfCookie+"="); 
  20.     if (begin != -1)   // Note: != means "is not equal to"
  21.    { 
  22.  
  23. // Our cookie was set. 
  24. // The value stored in the cookie is returned from the function.
  25.  
  26.      begin += NameOfCookie.length+1; 
  27.       end = document.cookie.indexOf(";", begin);
  28.       if (end == -1) end = document.cookie.length;
  29.       return unescape(document.cookie.substring(begin, end));       } 
  30.   }
  31. return null;  
  32.  
  33. // Our cookie was not set. 
  34. // The value "null" is returned from the function.
  35.  
  36. }
  37.  
  38. function setCookie(NameOfCookie, value, expiredays) 
  39. {
  40.  
  41. // Three variables are used to set the new cookie. 
  42. // The name of the cookie, the value to be stored,
  43. // and finaly the number of days till the cookie expires.
  44. // The first lines in the function converts 
  45. // the number of days to a valid date.
  46.  
  47.   var ExpireDate = new Date ();
  48.   ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
  49.  
  50. // The next line stores the cookie, simply by assigning 
  51. // the values to the "document.cookie"-object.
  52. // Note the date is converted to Greenwich Meantime using
  53. // the "toGMTstring()"-function.
  54.  
  55.   document.cookie = NameOfCookie + "=" + escape(value) + 
  56.   ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
  57. }
  58.  
  59.