[JavaScript] cookie

JavaScript 2009. 7. 1. 20:54



쿠키 저장하기

function setCookie(name, value, expiredays)
{
    // 만료일 (expiredays가 365면 만료일은 현재날짜로부터 365일)
    var exdate = new Date();
    exdate.setDate(exdate.getDate()+expiredays);

    document.cookie = name+"="+value+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}




쿠키값 가져오기

function getCookie(name)
{
    if (document.cookie.length>0) {
        var c_start = document.cookie.indexOf(name+"=");
        if (c_start!=-1) {
            c_start = c_start+name.length+1;
            var c_end  = document.cookie.indexOf(";", c_start);
            if (c_end==-1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}




Posted by bloodguy
,