home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 October / Chip_2000-10_cd1.bin / redakce / soutez / content / js / setConnected.js < prev    next >
Text File  |  1999-12-08  |  4KB  |  104 lines

  1. function FixCookieDate (date) {
  2.   var base = new Date(0);
  3.   var skew = base.getTime(); // dawn of (Unix) time - should be 0
  4.   if (skew > 0)  // Except on the Mac - ahead of its time
  5.     date.setTime (date.getTime() - skew);
  6. }
  7.  
  8. var expdate = new Date ();
  9. FixCookieDate (expdate); // Correct for Mac date bug - call only once for given Date object!
  10. expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000)); // 24 hrs from now 
  11.  
  12. //
  13. //  Function to create or update a cookie.
  14. //    name - String object containing the cookie name.
  15. //    value - String object containing the cookie value.  May contain
  16. //      any valid string characters.
  17. //    [expires] - Date object containing the expiration data of the cookie.  If
  18. //      omitted or null, expires the cookie at the end of the current session.
  19. //    [path] - String object indicating the path for which the cookie is valid.
  20. //      If omitted or null, uses the path of the calling document.
  21. //    [domain] - String object indicating the domain for which the cookie is
  22. //      valid.  If omitted or null, uses the domain of the calling document.
  23. //    [secure] - Boolean (true/false) value indicating whether cookie transmission
  24. //      requires a secure channel (HTTPS).  
  25. //
  26. //  The first two parameters are required.  The others, if supplied, must
  27. //  be passed in the order listed above.  To omit an unused optional field,
  28. //  use null as a place holder.  For example, to call SetCookie using name,
  29. //  value and path, you would code:
  30. //
  31. //      SetCookie ("myCookieName", "myCookieValue", null, "/");
  32. //
  33. //  Note that trailing omitted parameters do not require a placeholder.
  34. //
  35. //  To set a secure cookie for path "/myPath", that expires after the
  36. //  current session, you might code:
  37. //
  38. //      SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);
  39. //
  40.  
  41. function SetCookie (name,value,expires,path,domain,secure) {
  42.   document.cookie = name + "=" + escape (value) +
  43.     ((expires) ? "; expires=" + expires.toGMTString() : "") +
  44.     ((path) ? "; path=" + path : "") +
  45.     ((domain) ? "; domain=" + domain : "") +
  46.     ((secure) ? "; secure" : "");
  47. }
  48.  
  49. //
  50. // Cookie Connected
  51. //   On /content/main.htm, we will ask them if they are
  52. //   connected to the internet or not.  Buttons for yes or
  53. //   no will be tied to javascript calls that will set
  54. //   another cookie called 'connected' to remember this
  55. //   answer.  
  56. //
  57. //   Whenever there is a link to any external site, the
  58. //   url must be passed to a function that will check the
  59. //   'connected' cookie (this function should also appear
  60. //   in /content/js/shared.js).  If they are connected to the
  61. //   internet, a simple redirection occurs.  If not, a
  62. //   warning dialog comes up asking them if they want to
  63. //   continue. A yes answer to the dialog box redirects
  64. //   them to the URL.  A no answer simply makes the dialog
  65. //   go away leaving them on the original page.
  66.  
  67. function clickBox(value) {
  68.    SetCookie ("Connected", value, expdate);
  69. }
  70.  
  71.  
  72.  
  73. // "Internal" function to return the decoded value of a cookie
  74. //
  75.  
  76. function getCookieVal (offset) {
  77.   var endstr = document.cookie.indexOf (";", offset);
  78.   if (endstr == -1)
  79.     endstr = document.cookie.length;
  80.   return unescape(document.cookie.substring(offset, endstr));
  81. }
  82.  
  83. //
  84. //  Function to return the value of the cookie specified by "name".
  85. //    name - String object containing the cookie name.
  86. //    returns - String object containing the cookie value, or null if
  87. //      the cookie does not exist.
  88. //
  89.  
  90. function GetCookie (name) {
  91.   var arg = name + "=";
  92.   var alen = arg.length;
  93.   var clen = document.cookie.length;
  94.   var i = 0;
  95.   while (i < clen) {
  96.     var j = i + alen;
  97.     if (document.cookie.substring(i, j) == arg)
  98.       return getCookieVal (j);
  99.     i = document.cookie.indexOf(" ", i) + 1;
  100.     if (i == 0) break; 
  101.   }
  102.   return null;
  103. }
  104.