topical media & game development

talk show tell print

mashup-amazon-10-10-02-Wishlist-js-cookie.js / js



  // JScript File for the cookies
  function CreateCookie(myName, myValue, numDays)
  {
          var myDate = new Date();
  
          // Define expiration as a number of milliseconds;
          var myTimeToLive = numDays * 24 * 60 * 60 * 1000;
  
          // set the time to the expiration time
          myDate.setTime(myDate.getTime() + myTimeToLive);
  
          // Convert to a string
          var myExpiration = "; expires=" + myDate.toGMTString();
  
          // Write out the cookie
          document.cookie = myName + "=" + myValue + myExpiration + "; path=/";
  }
  
  function ReadCookie(myName)
  {
          // Obtain an array of cookie name-values
          var objCookies = document.cookie.split(';');        
  
          var myNameTag = myName + "=";
  
          // Search the cookie array for the one you're looking for. 
          for(var i=0 ; i < objCookies.length; i++)
          {
                  var myCookie = objCookies[i];
                  
                  // Need to trim the cookie of any leading spaces
                  while (myCookie.charAt(0)==' ') myCookie = myCookie.substring(1, myCookie.length);
                  // Does this match? 
                  if (myCookie.indexOf(myNameTag) == 0) {
                          return myCookie.substring( myNameTag.length, myCookie.length);
                  }        
          }
          return null;
  }
  
  function EraseCookie(name)
  {
          CreateCookie(name,"",-1);
  }
  
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.