home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Java / ScriptBuilder / NOSB30_TRIAL.exe / data1.cab / Program_Files / CompLib / cookie.js < prev    next >
Encoding:
Text File  |  1998-10-05  |  4.9 KB  |  111 lines

  1. function com_netobjects_cookieComponent( params ) {
  2.     this.getCookie = ccGetCookie;
  3.     this.setCookie = ccSetCookie;
  4.     this.deleteCookie = ccDeleteCookie;
  5.    this.fixDate = fixDate;
  6.    this.setcookieValue = setcookieValue
  7.  
  8.     this.name = (params.name+"" != "undefined" ? params.name : "cookieComponent1");
  9.     this.cookieName = (params.cookieName+"" != "undefined" ? params.cookieName : "");
  10.    this.value = (params.value+"" != "undefined" ? params.value : "");
  11.     this.year = (params.year+"" != "undefined" ? params.year : "");
  12.     this.month = (params.month+"" != "undefined" ? params.month : "");
  13.     this.day = (params.day+"" != "undefined" ? params.day : "");
  14.     this.path = (params.path+"" != "undefined" ? params.path : "");
  15.     this.domain = (params.domain+"" != "undefined" ? params.domain : "");
  16.     this.secure = (params.secure+"" != "undefined" ? params.secure : "");
  17.  
  18.     // Boolean variable specified if alert should be displayed if cookie exceeds 4KB
  19.     ccCaution = false;
  20.     
  21.    function setcookieValue(newCookie, name) {
  22.       if(newCookie+"" != "null" && newCookie+"" != "undefined")
  23.           eval(name + ".value=newCookie;");
  24.       else
  25.          alert("You need to specify an appropriate name for the cookie");
  26.    }
  27.  
  28.     // date - any instance of the Date object
  29.     // * you should hand all instances of the Date object to this function for "repairs"
  30.     // * this function is taken from Chapter 14, "Time and Date in JavaScript", in "Learn Advanced JavaScript Programming"
  31.     function fixDate(date) {        
  32.         var base = new Date(0);
  33.        var skew = base.getTime(); 
  34.        if (skew > 0)
  35.            date.setTime(date.getTime() - skew);
  36.     }
  37.     
  38.     // name - name of the cookie// value - value of the cookie
  39.     // [expires] - expiration date of the cookie (defaults to end of current session)
  40.     // [path] - path for which the cookie is valid (defaults to path of calling document)
  41.     // [domain] - domain for which the cookie is valid (defaults to domain of calling document)
  42.     // [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
  43.     // * an argument defaults when it is assigned null as a placeholder
  44.     // * a null placeholder is not required for trailing omitted arguments
  45.     function ccSetCookie(name) {
  46.          if(eval(name + ".cookieName") != "" && 
  47.             eval(name + ".cookieName")+"" != "undefined" && 
  48.             eval(name + ".cookieName")+"" != "null")
  49.          {
  50.               var newExpiry = new Date(eval(name + ".year"), eval(name + ".month"), eval(name + ".day"));
  51.               eval("\"" + name + ".fixDate(" + newExpiry + ")\"");
  52.             var curCookie = eval(name + ".cookieName") + "=" + 
  53.                    escape(eval(name + ".value")) + 
  54.                         (newExpiry ? "; expires=" + newExpiry.toGMTString() : "") +
  55.                     (eval(name + ".path") ? "; path=" + eval(name + ".path") : "") +
  56.                     (eval(name + ".domain") ? "; domain=" + eval(name + ".domain") : "") +
  57.                     (eval(name + ".secure") ? "; secure" : "");
  58.                     alert("curCookie: " + curCookie);
  59.             if (!ccCaution || (eval(name + ".cookieName") + "=" + eval(name + ".value")).length <= 4000)
  60.                     document.cookie = curCookie;        
  61.             else if (confirm("Cookie exceeds 4KB and will be cut!"))
  62.                     document.cookie = curCookie;
  63.          }
  64.     }    
  65.                         
  66.     // name - name of the desired cookie
  67.     // * return string containing value of specified cookie or null if cookie does not exist
  68.     function ccGetCookie(name) {        
  69.         if(eval(name + ".cookieName") != "" || 
  70.            eval(name + ".cookieName")+"" != "undefined" && 
  71.            eval(name + ".cookieName")+"" != "null")
  72.         {
  73.         var cname = eval(name + ".cookieName") + "=";
  74.         var dc = document.cookie;
  75.         if(dc.length > 0) {
  76.           begin = dc.indexOf(cname);
  77.           if(begin != -1) {
  78.             begin += cname.length;
  79.             end = dc.indexOf(";", begin);
  80.             if(end == -1) end = dc.length;
  81.               return unescape(dc.substring(begin, end));
  82.           }
  83.           return null;
  84.           }
  85.         }
  86.     }
  87.     
  88.     // name - name of the cookie
  89.     // [path] - path of the cookie (must be same as path used to create cookie)
  90.     // [domain] - domain of the cookie (must be same as domain used to create cookie)
  91.     // * path and domain default if assigned null or omitted if no explicit argument proceeds
  92.     function ccDeleteCookie(name) {        
  93.         if(eval(name + ".cookieName") != "" || 
  94.            eval(name + ".cookieName")+"" != "undefined" && 
  95.            eval(name + ".cookieName")+"" != "null")
  96.         {
  97.             if (ccGetCookie(eval(name + ".cookieName"))) {
  98.              curCookie = eval(name + ".cookieName") + "=" + 
  99.                  ((eval(name + ".path")) ? "; path=" + eval(name + ".path") : "") +
  100.                  ((eval(name + ".domain")) ? "; domain=" + eval(name + ".domain") : "") +
  101.                  "; expires=Thu, 01-Jan-70 00:00:01 GMT"   
  102.              alert("curCookie: " + curCookie);
  103.              document.cookie = curCookie;
  104.                return true;
  105.           }
  106.             return false;
  107.         }
  108.         return false;
  109.     }
  110. }
  111.