Return to Snippet

Revision: 48460
at July 1, 2011 22:13 by devnull69


Initial Code
function Trim(strValue) {
  return strValue.replace(/^\s+|\s+$/g, '');
}

function getCookie(key) {
   var result = false;
   if(document.cookie) {
      var mycookieArray = document.cookie.split(';');
      for(i=0; i<mycookieArray.length; i++) {
         var mykeyValue = mycookieArray[i].split('=');
         if(Trim(mykeyValue[0]) == key) result = mykeyValue[1];
      }
   }
   return result;
}

function setCookie(key, value, hoursExpire) {
   var ablauf = new Date();
   var expireTime = ablauf.getTime() + (hoursExpire * 60 * 60 * 1000);
   ablauf.setTime(expireTime);
   document.cookie = key + "=" + value + "; expires=" + ablauf.toGMTString();
}

var mycookie = getCookie('meincookie');
if(!mycookie) {
   // do whatever you want if the cookie is not set
   setCookie('meincookie', 'true', 24);
} else {
   // do whatever you want if the cookie is set
   // mycookie contains the cookie value
}

Initial URL

                                

Initial Description

                                

Initial Title
Get and Set Cookie

Initial Tags

                                

Initial Language
JavaScript