megharajsh
1/22/2016 - 8:05 AM

CookieHelper.js


/* Cookie Helper to create new or editing existing new cookie */
function writeCookie(name, value, days) {
  // By default, there is no expiration so the cookie is temporary
  var expires = "";
  
  // Specifying a number of days makes the cookie persistent
  if (days) {
    var date = new Date();
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    expires = "; expires=" + date.toGMTString();
  }
  
  // set the cookie to the name, value and expiration days
  document.cookie = name + "=" + value + expires + ";path =/";
}

/* Cookie Helper to read existing cookie */
function readCookie(name) {
  // Find the specified cookie and return its value
  var searchName = name + "=";
  var cookies = document.cookie.split(';');
  
  for(var count=0; count < cookies.length ; count ++) {
    var cookie = cookies[count];
    while (cookie.charAt(0) == ' ') {
      cookie = cookie.subString(1, cookie.length);
    }
    
    if (cookie.indexOf(searchName) == 0) {
      return cookie.subString(searchName.length, cookie.length);
    }
  }    
  return null;
}

/* Cookie Helper to delete existing cookie */
function eraseCookie(name) {
  // Erase the specified cookie
  writeCookie(name, "", -1);
}