IT_Programming/JavaScript

쿠키 생성, 삭제

JJun ™ 2006. 4. 26. 01:38

<script language=JavaScript>

// 쿠키생성
function setnull( name, value, expiredays )
{
 var today = new Date();
 today.setDate( today.getDate() + expiredays );
 doc-ument.coo-kie = name + "=" + escape( value ) + "; path=/; expires=" + today.toGMTString() + ";";
}

// 쿠키구하기
function getnull(name)
{
    coo-kie  = doc-ument.coo-kie;
    name    = name + "=";
    idx     = coo-kie.indexOf(name);
    if(coo-kie && idx >= 0) {       
       
        tmp     = coo-kie.substring(idx, coo-kie.length);
        deli    = tmp.indexOf(";");
        if(deli > 0) {
            return tmp.substring(name.length, deli);
        } else {
            return tmp.substring(name.length);
        }
       
    }
}

// 쿠키지우기
function clearnull(name)
{
    today   = new Date();
    today.setDate(today.getDate() - 1);
    doc-ument.coo-kie = name + "=; path=/; expires=" + today.toGMTString() + ";";
}
// 모든쿠키 삭제
function clearAllnull()
{
    coo-kie  = doc-ument.coo-kie.split(";");
    total   = coo-kie.length;
    for(i=0; i<total; i++) {
    
        name = coo-kie[i].substring(0, coo-kie[i].indexOf("="));
        clearnull(name);
    }
}
      
// 쿠키확인
function allnull()
{
    alert(doc-ument.coo-kie);
}
</script>
<input type="button" value="전체쿠키확인" on-click="allnull()">
<input type="button" value="전체쿠키삭제" on-click="clearAllnull()">
<br>
<input type="button" value="쿠키생성1" on-click="setnull('test','done',1)">
<input type="button" value="쿠키생성2" on-click="setnull('test2','done2',1)">
<br>
<input type="button" value="쿠키확인1" on-click="alert(getnull('test'))">
<input type="button" value="쿠키확인2" on-click="alert(getnull('test2'))">
<br>
<input type="button" value="쿠키삭제1" on-click="clearnull('test')">
<input type="button" value="쿠키삭제2" on-click="clearnull('test2')">