프런트엔드 설정, 가져오기, 삭제

쿠키 설정
/**
 * [setCookie    cookie]
 * @author	 Leo
 * @param    {[type]}                 name   [   ,  ]
 * @param    {[type]}                 value  [   ,  ]
 * @param    {[type]}                 exdays [    ,  ]
 */
function setCookie(name, value, exdays) {
  let cookie = name + "=" + value + ";";
  if (exdays) {
    let d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    let expires = "expires=" + d.toGMTString();
    cookie = cookie + ' ' + expires;
  }
  document.cookie = cookie;
}

쿠키 가져오기
/**
 * [getCookie    cookie]
 * @author	 Leo
 * @param    {[type]}                 cname [   ,  ]
 * @return   {[type]}                 [   :   (   )]
 */
function getCookie(cname) {
  let name = cname + "=";
  let cookie = document.cookie.split(';');
  for(let i = 0, len = cookie.length; i < len; i++) {
    let c = cookie[i].trim();
    if (c.indexOf(name) === 0) {
      return c.substring(name.length, c.length);
    }
  }
  return "";
}

쿠키 삭제
/**
 * [getCookie    cookie]
 * @author	 Leo
 * @param    {[type]}                 key[   ,  ]
 */
function clearCookieByKey(key) {
   setCookie(key, "", -1);
 }

좋은 웹페이지 즐겨찾기