home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 25: Programming / pc_actual_25.iso / Javascript / InteractiveWebDesignJavascript / Scripting / js2 / labs / cookies / cookie_stuff.js < prev    next >
Encoding:
JavaScript  |  2001-07-18  |  2.6 KB  |  82 lines

  1. /******************************************************
  2. ** ** These functions read, write and delete cookies
  3. ** Most functions were ported from http://builder.com
  4. ** But the dater routine is custom code that builds the
  5. ** date in a Y2K compliant format that allows you to
  6. ** do a general number comparison to see if a date is
  7. ** greater than another or not
  8. *******************************************************/
  9. /*
  10. ** Declare & initialize variables
  11. */
  12. //set to expire on December 31, 2001 at 11:59:59
  13. var _EXPIRATION_ = new Date();
  14. _EXPIRATION_.setTime(1009871999259);
  15.  
  16. //get today's date
  17. var _TODAY_ = new Number(dater())
  18. //alert ("today:" + _TODAY_);
  19.  
  20. /*
  21. ** Get today's date and format it nicely
  22. */
  23. function dater()
  24. {
  25.     //declare variables
  26.     var _TODAY_ = new Date()
  27.     tempday = _TODAY_.getDate()
  28.     tempday1 = new Number(tempday)
  29.     tempmonth = _TODAY_.getMonth()
  30.     tempyear = _TODAY_.getYear()
  31.     tempmonth1 = new Number(tempmonth)
  32.     tempmonth1++
  33.  
  34.     //format day: if less than 10, add a zero in front
  35.     if (tempday1 < 10)    day = new String('0' + tempday1)
  36.     else day = new String(tempday1)
  37.  
  38.     //format month: if less than 10, add a zero in front
  39.     if (tempmonth1 < 10) month = new String('0' + tempmonth1)
  40.     else month = new String(tempmonth1)
  41.     
  42.     //format year: add 19 to front of '99
  43.     if (tempyear < 2000) year = new String('19' + tempyear)
  44.     else year = tempyear
  45.         
  46.     //format full date
  47.     date = year +  month + day 
  48.     //alert ('dater: ' + date)
  49.     return date;
  50. }
  51.  
  52. /*
  53. ** cookie functions
  54. */
  55. // Heinle's function for retrieving a cookie.
  56. function getCookie(name){
  57.   var cname = name + "=";               
  58.   var dc = document.cookie;             
  59.   if (dc.length > 0) {              
  60.     begin = dc.indexOf(cname);       
  61.     if (begin != -1) {           
  62.       begin += cname.length;       
  63.       end = dc.indexOf(";", begin);
  64.       if (end == -1) end = dc.length;
  65.         return unescape(dc.substring(begin, end));
  66.     } 
  67.   }
  68.   return null;
  69. }
  70.  
  71. // An adaptation of Dorcht's function for setting a cookie.
  72. function setCookie(name, value, expires, path, domain, secure) {
  73.   document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : "; expires=" + expires.toGMTString()) + ((path == null) ? "" : "; path=" + path) + ((domain == null) ? "" : "; domain=" + domain) + ((secure == null) ? "" : "; secure");
  74. }
  75.  
  76. // An adaptation of Dorcht's function for deleting a cookie.
  77. function delCookie (name,path,domain) {
  78.   if (getCookie(name)) {
  79.     document.cookie = name + "=" + ((path == null) ? "" : "; path=" + path) + ((domain == null) ? "" : "; domain=" + domain) + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  80.   }
  81. }
  82.