home *** CD-ROM | disk | FTP | other *** search
/ Web Designer 98 (Professional) / WebDesigner 1.0.iso / htmled1 / hs25set.exe / COOKIES.JS < prev    next >
Encoding:
Text File  |  1996-12-12  |  8.1 KB  |  200 lines

  1. <html>
  2. <head>
  3. <title>Cookie Functions</title>
  4. </head>
  5. <body>
  6. <script language="javascript">
  7. <!-- begin script
  8. //
  9. //  Cookie Functions -- "Night of the Living Cookie" Version (25-Jul-96)
  10. //
  11. //  Written by:  Bill Dortch, hIdaho Design 
  12. //  The following functions are released to the public domain.
  13. //  The latest versions of these functions can always be found at 
  14. //  http://www.hidaho.com/cookies/cookie.txt.
  15. //
  16. //  This version takes a more aggressive approach to deleting
  17. //  cookies.  Previous versions set the expiration date to one
  18. //  millisecond prior to the current time; however, this method
  19. //  did not work in Netscape 2.02 (though it does in earlier and
  20. //  later versions), resulting in "zombie" cookies that would not
  21. //  die.  DeleteCookie now sets the expiration date to the earliest
  22. //  usable date (one second into 1970), and sets the cookie's value
  23. //  to null for good measure.
  24. //
  25. //  Also, this version adds optional path and domain parameters to
  26. //  the DeleteCookie function.  If you specify a path and/or domain
  27. //  when creating (setting) a cookie**, you must specify the same
  28. //  path/domain when deleting it, or deletion will not occur.
  29. //
  30. //  The FixCookieDate function must now be called explicitly to
  31. //  correct for the 2.x Mac date bug.  This function should be
  32. //  called *once* after a Date object is created and before it
  33. //  is passed (as an expiration date) to SetCookie.  Because the
  34. //  Mac date bug affects all dates, not just those passed to
  35. //  SetCookie, you might want to make it a habit to call
  36. //  FixCookieDate any time you create a new Date object:
  37. //
  38. //    var theDate = new Date();
  39. //    FixCookieDate (theDate);
  40. //
  41. //  Calling FixCookieDate has no effect on platforms other than
  42. //  the Mac, so there is no need to determine the user's platform
  43. //  prior to calling it.
  44. //
  45. //  This version also incorporates several minor coding improvements.
  46. //
  47. //  **Note that it is possible to set multiple cookies with the same
  48. //  name but different (nested) paths.  For example:
  49. //
  50. //    SetCookie ("color","red",null,"/outer");
  51. //    SetCookie ("color","blue",null,"/outer/inner");
  52. //
  53. //  However, GetCookie cannot distinguish between these and will return
  54. //  the first cookie that matches a given name.  It is therefore
  55. //  recommended that you *not* use the same name for cookies with
  56. //  different paths.  (Bear in mind that there is *always* a path
  57. //  associated with a cookie; if you don't explicitly specify one,
  58. //  the path of the setting document is used.)
  59. //  
  60. //  Revision History:
  61. //
  62. //    "Toss Your Cookies" Version (22-Mar-96)
  63. //      - Added FixCookieDate() function to correct for Mac date bug
  64. //
  65. //    "Second Helping" Version (21-Jan-96)
  66. //      - Added path, domain and secure parameters to SetCookie
  67. //      - Replaced home-rolled encode/decode functions with Netscape's
  68. //        new (then) escape and unescape functions
  69. //
  70. //    "Free Cookies" Version (December 95)
  71. //
  72. //
  73. //  For information on the significance of cookie parameters, and
  74. //  and on cookies in general, please refer to the official cookie
  75. //  spec, at:
  76. //
  77. //      http://www.netscape.com/newsref/std/cookie_spec.html    
  78. //
  79. //******************************************************************
  80. //
  81. // "Internal" function to return the decoded value of a cookie
  82. //
  83. function getCookieVal (offset) {
  84.   var endstr = document.cookie.indexOf (";", offset);
  85.   if (endstr == -1)
  86.     endstr = document.cookie.length;
  87.   return unescape(document.cookie.substring(offset, endstr));
  88. }
  89. //
  90. //  Function to correct for 2.x Mac date bug.  Call this function to
  91. //  fix a date object prior to passing it to SetCookie.
  92. //  IMPORTANT:  This function should only be called *once* for
  93. //  any given date object!  See example at the end of this document.
  94. //
  95. function FixCookieDate (date) {
  96.   var base = new Date(0);
  97.   var skew = base.getTime(); // dawn of (Unix) time - should be 0
  98.   if (skew > 0)  // Except on the Mac - ahead of its time
  99.     date.setTime (date.getTime() - skew);
  100. }
  101. //
  102. //  Function to return the value of the cookie specified by "name".
  103. //    name - String object containing the cookie name.
  104. //    returns - String object containing the cookie value, or null if
  105. //      the cookie does not exist.
  106. //
  107. function GetCookie (name) {
  108.   var arg = name + "=";
  109.   var alen = arg.length;
  110.   var clen = document.cookie.length;
  111.   var i = 0;
  112.   while (i < clen) {
  113.     var j = i + alen;
  114.     if (document.cookie.substring(i, j) == arg)
  115.       return getCookieVal (j);
  116.     i = document.cookie.indexOf(" ", i) + 1;
  117.     if (i == 0) break; 
  118.   }
  119.   return null;
  120. }
  121. //
  122. //  Function to create or update a cookie.
  123. //    name - String object containing the cookie name.
  124. //    value - String object containing the cookie value.  May contain
  125. //      any valid string characters.
  126. //    [expires] - Date object containing the expiration data of the cookie.  If
  127. //      omitted or null, expires the cookie at the end of the current session.
  128. //    [path] - String object indicating the path for which the cookie is valid.
  129. //      If omitted or null, uses the path of the calling document.
  130. //    [domain] - String object indicating the domain for which the cookie is
  131. //      valid.  If omitted or null, uses the domain of the calling document.
  132. //    [secure] - Boolean (true/false) value indicating whether cookie transmission
  133. //      requires a secure channel (HTTPS).  
  134. //
  135. //  The first two parameters are required.  The others, if supplied, must
  136. //  be passed in the order listed above.  To omit an unused optional field,
  137. //  use null as a place holder.  For example, to call SetCookie using name,
  138. //  value and path, you would code:
  139. //
  140. //      SetCookie ("myCookieName", "myCookieValue", null, "/");
  141. //
  142. //  Note that trailing omitted parameters do not require a placeholder.
  143. //
  144. //  To set a secure cookie for path "/myPath", that expires after the
  145. //  current session, you might code:
  146. //
  147. //      SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);
  148. //
  149. function SetCookie (name,value,expires,path,domain,secure) {
  150.   document.cookie = name + "=" + escape (value) +
  151.     ((expires) ? "; expires=" + expires.toGMTString() : "") +
  152.     ((path) ? "; path=" + path : "") +
  153.     ((domain) ? "; domain=" + domain : "") +
  154.     ((secure) ? "; secure" : "");
  155. }
  156.  
  157. //  Function to delete a cookie. (Sets expiration date to start of epoch)
  158. //    name -   String object containing the cookie name
  159. //    path -   String object containing the path of the cookie to delete.  This MUST
  160. //             be the same as the path used to create the cookie, or null/omitted if
  161. //             no path was specified when creating the cookie.
  162. //    domain - String object containing the domain of the cookie to delete.  This MUST
  163. //             be the same as the domain used to create the cookie, or null/omitted if
  164. //             no domain was specified when creating the cookie.
  165. //
  166. function DeleteCookie (name,path,domain) {
  167.   if (GetCookie(name)) {
  168.     document.cookie = name + "=" +
  169.       ((path) ? "; path=" + path : "") +
  170.       ((domain) ? "; domain=" + domain : "") +
  171.       "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  172.   }
  173. }
  174.  
  175. //
  176. //  Examples
  177. //
  178. var expdate = new Date ();
  179. FixCookieDate (expdate); // Correct for Mac date bug - call only once for given Date object!
  180. expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000)); // 24 hrs from now 
  181. SetCookie ("ccpath", "http://www.hidaho.com/colorcenter/", expdate);
  182. SetCookie ("ccname", "hIdaho Design ColorCenter", expdate);
  183. SetCookie ("tempvar", "This is a temporary cookie.");
  184. SetCookie ("ubiquitous", "This cookie will work anywhere in this domain",null,"/");
  185. SetCookie ("paranoid", "This cookie requires secure communications",expdate,"/",null,true);
  186. SetCookie ("goner", "This cookie must die!");
  187. document.write (document.cookie + "<br>");
  188. DeleteCookie ("goner");
  189. document.write (document.cookie + "<br>");
  190. document.write ("ccpath = " + GetCookie("ccpath") + "<br>");
  191. document.write ("ccname = " + GetCookie("ccname") + "<br>");
  192. document.write ("tempvar = " + GetCookie("tempvar") + "<br>");
  193. // end script -->
  194. </script>
  195. </body>
  196. </html>
  197.  
  198.  
  199.  
  200.