home *** CD-ROM | disk | FTP | other *** search
/ Dynamic HTML in Action / Dynamicke-HTML-v-akci-covermount.bin / Tools / cookies.js next >
Encoding:
JavaScript  |  1998-12-20  |  1.4 KB  |  36 lines

  1. //----------------------------
  2. // This js file contains basic tools for cookie manipulation
  3.  
  4. function SetCookie(sName,sVal,sExpires,sDomain,sPath,bSecure) {
  5. //-------------------------------------
  6. // Set a cookie.
  7.   document.cookie = ( sName + "=" + escape(sVal) +
  8.      ( (sExpires) ? "; expires=" + sExpires : "" ) +
  9.      ( (sDomain) ? "; domain=" + sDomain : "" ) +
  10.      ( (sPath) ? "; path=" + sPath : "" ) +
  11.      ( (bSecure) ? "; secure" : "" )    );
  12. }
  13.  
  14. function GetCookie(sName){
  15. //-------------------------------------
  16. // Traverse the cookie, looking for the passed cookie name.
  17. // If found, return the value, else return null.
  18.   var sDelimName   = ( sName + "=" );
  19.   var iNameLength  = sDelimName.length;
  20.   var iCookieLength= document.cookie.length;
  21.   var iNameStart = 0;
  22.   while (iNameStart < iCookieLength){
  23.     var iNameEnd = (iNameStart + iNameLength);
  24.     if (document.cookie.substring(iNameStart, iNameEnd) == sDelimName){
  25.       // If name found, get the associated value
  26.       // (from iNameEnd  to next ; or end of cookie)
  27.       var iValueEnd = document.cookie.indexOf (";", iNameEnd);
  28.       if (iValueEnd == -1){ iValueEnd = document.cookie.length }
  29.       return unescape(document.cookie.substring(iNameEnd, iValueEnd));
  30.     }
  31.     iNameStart = ( document.cookie.indexOf(" ", iNameStart) + 1 );
  32.     if (iNameStart == 0) break; 
  33.   }
  34.   return null;
  35. }
  36.