Cookie 생성 조회 삭제

By | 2020년 7월 17일
Table of Content

Cookie 생성 조회 삭제

/*
setCookie("test", "test1234", 1*60*60);
alert(getCookie("test"));
deleteCookie("test");
alert(getCookie("test"));
*/
var setCookie = function(name, value, sec) {
    var date = new Date();
    date.setTime(date.getTime() + (sec * 1000));
    document.cookie = name + '=' + value + ';expires=' + date.toUTCString() + ';path=/';
};

var getCookie = function(name) {
    var value = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
    return value? value[2] : null;
};

var deleteCookie = function(name) {
    var date = new Date();
    document.cookie = name + "= " + "; expires=" + date.toUTCString() + "; path=/";
}

답글 남기기