home *** CD-ROM | disk | FTP | other *** search
/ Excalibur 71 / Excalibur_71_cd2.bin / Microsoft_Internet_50 / ieak5.exe / RCDATA / CABINET / cookie.js < prev    next >
Text File  |  1999-02-24  |  3KB  |  90 lines

  1. /*
  2. WM_setCookie(), WM_readCookie(), WM_killCookie(), WM_acceptsCookies()
  3. A set of functions that eases the pain of using cookies.
  4.  
  5. Source: Webmonkey Code Library
  6. (http://www.hotwired.com/webmonkey/javascript/code_library/)
  7.  
  8. Author: Nadav Savio
  9. Author Email: nadav@wired.com
  10.  
  11. Usage: 
  12.  
  13. WM_setCookie('name', 'value'[, hours, 'path', 'domain', secure]);
  14. where name, value, and path are strings, and secure is either true or null. Only name and value are required.
  15.  
  16. WM_readCookie('name');
  17. Returns the value associated with name.
  18.  
  19. WM_getCookieValue('name');
  20. This is a helper function used by WM_readCookie() and WM_killCookie(). It extracts a single value based on name, given a cookie of the form 'name=value'.
  21. WM_killCookie('name'[, 'path', 'domain']);
  22. Remember that path and domain must be supplied if they were set with the cookie.
  23.  
  24. WM_acceptsCookies();
  25. Returns true or false.
  26. */
  27.  
  28. function WM_acceptsCookies() {
  29.   // This function tests whether the user accepts cookies.
  30.   // Declare variable.
  31.   var answer;
  32.   // Try to set a cookie.
  33.   document.cookie = 'WM_acceptsCookies=yes';
  34.   // If it fails, return false; if it succeeds, return true.
  35.   if(document.cookie == '') answer = false; else answer = true;
  36.   // Then clean up by expiring the cookie.
  37.   document.cookie = 'WM_acceptsCookies=yes; expires=Fri, 13-Apr-1970 00:00:00 GMT';
  38.   return answer;
  39. }
  40.  
  41. function WM_setCookie (name, value, hours, path, domain, secure) {
  42.   // Don't waste your time if the browser doesn't accept cookies.
  43.   if (WM_acceptsCookies()) {
  44.     // Set the cookie, adding any parameters that were specified.
  45.     // (Convert hours to milliseconds (*3600000) 
  46.     // and then to a GMTString.)
  47.     document.cookie = name + '=' + escape(value) + ((hours)?(';expires=' + ((new Date((new Date()).getTime() + hours*3600000)).toGMTString())):'') + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'') + ((secure && (secure == true))?'; secure':'');
  48.   }
  49. }
  50.  
  51. function WM_readCookie(name) {
  52.   // if there's no cookie, return false else get the value and return it
  53.   if(document.cookie == '') return false; 
  54.   else return unescape(WM_getCookieValue(name));
  55. }
  56.  
  57. function WM_getCookieValue(name) {
  58.   // Declare variables.
  59.   var firstChar, lastChar;
  60.   // Get the entire cookie string. (This may have other name=value pairs in it.)
  61.   var theBigCookie = document.cookie;
  62.   // Grab just this cookie from theBigCookie string.
  63.   // Find the start of 'name'.
  64.   firstChar = theBigCookie.indexOf(name);
  65.   // If you found it,
  66.   if(firstChar != -1) {
  67.     // skip 'name' and '='.
  68.     firstChar += name.length + 1;
  69.     // Find the end of the value string (i.e. the next ';').
  70.     lastChar = theBigCookie.indexOf(';', firstChar);
  71.     if(lastChar == -1) lastChar = theBigCookie.length;
  72.     // Return the value.
  73.     return theBigCookie.substring(firstChar, lastChar);
  74.   } else {
  75.     // If there was no cookie, return false.
  76.     return false;
  77.   }
  78. }
  79.  
  80. function WM_killCookie(name, path, domain) {
  81.   // We'll need the name and the value to kill the cookie, so get the value.
  82.   var theValue = WM_getCookieValue(name);
  83.   // Assuming there actually is such a cookie.
  84.   if(theValue) {
  85.     // Set an expired cookie, adding 'path' and 'domain' 
  86.     // if they were given.
  87.     document.cookie = name + '=' + theValue + '; expires=Fri, 13-Apr-1970 00:00:00 GMT' + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'');
  88.   }
  89. }
  90.