잡지식

스크립트 단에서 쿠키제어

엉으니 2017. 2. 24. 15:21


스크립트 단에서 쿠키제어

function setCookie(cookieName, value, exdays){

    var exdate = new Date();

    exdate.setDate(exdate.getDate() + exdays);

    var cookieValue = escape(value) + ((exdays==null) ? "" : "; expires=" + exdate.toGMTString());

    document.cookie = cookieName + "=" + cookieValue;

}

 

function deleteCookie(cookieName){

    var expireDate = new Date();

    expireDate.setDate(expireDate.getDate() - 1);

    document.cookie = cookieName + "= " + "; expires=" + expireDate.toGMTString();

}

 

function getCookie(cookieName) {

    cookieName = cookieName + '=';

    var cookieData = document.cookie;

    var start = cookieData.indexOf(cookieName);

    var cookieValue = '';

    if(start != -1){

        start += cookieName.length;

        var end = cookieData.indexOf(';', start);

        if(end == -1)end = cookieData.length;

        cookieValue = cookieData.substring(start, end);

    }

    return unescape(cookieValue);

반응형