728x90

안녕하세요. 공유민입니다.

쿠키 생성, 삭제 및 확인 예제 소스로 정리해봤습니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<!DOCTYPE html>
<head>
    <meta charset="utf-8"/>
    <script>                
 
        // 해당 이름의 쿠키를 삭제한다.
        function delCookie(_name)
        {
            var expireDate = new Date();
          
            //어제 날짜를 쿠키 소멸 날짜로 설정한다.
            expireDate.setDate( expireDate.getDate() - 1 );
            document.cookie = _name + "= " + "; expires=" + expireDate.toGMTString() + "; path=/";
            
            alert(document.cookie);
        }       
        
        // 이름, 값, 시간(초)로 쿠키를 생성한다.
        function setCookie(_name, _value, _time)
        {
            var cookie = _name + "=" + escape(_value) + "; path=/;"
            if (typeof _date != 'undefined'
            {
                var todayDate = new Date();
                //todayDate.setDate(todayDate.getDate() + _date); // 날짜
                todayDate.setTime(todayDate.getTime() + 60*1000*_time); // 시간 (milli seconds)
                cookie += "expires=" + todayDate.toGMTString() + ";"
            }
            document.cookie = cookie;
            
            alert(document.cookie);
        }
        
        
        // 해당 이름의 쿠키를 확인한다.
        function getCookie(_name) 
        {
            try 
            {
                var nameOfCookie = _name + "="// 'test='
                var x = 0;
                while (x <= document.cookie.length
                {
                   var y = (x + nameOfCookie.length); // 시작값 + 쿠키이름 길이
                   if (document.cookie.substring(x, y) == nameOfCookie) // _name과 일치하는 경우
                   {
                      if ((endOfCookie = document.cookie.indexOf";", y )) == -1// 구분자(;)를 못 찾은 경우
                           endOfCookie = document.cookie.length;
                      return alert(document.cookie.substring(y, endOfCookie));
                   }
                   x = document.cookie.indexOf(" ", x) + 1// 쿠키 구분자로 이동 (; 후 공백)
                   if (x == 0break// 이후 다른 쿠키가 없는 경우 종료
                }
            }
            catch(err) 
            {
                alert("getCookie[" + err.description + "]");
            }
            alert();
        }
     
    </script>
</head>
    
<body>
    <input type="button" onclick="setCookie('test1', 'ok', 600);" value="test1 쿠키생성">
    <input type="button" onclick="delCookie('test1');" value="test1 쿠키삭제">
    <input type="button" onclick="getCookie('test1');" value="test1 쿠키확인">
    </br>
    </br>
    <input type="button" onclick="setCookie('test2', 'ok', 600);" value="test2 쿠키생성">
    <input type="button" onclick="delCookie('test2');" value="test2 쿠키삭제">
    <input type="button" onclick="getCookie('test2');" value="test2 쿠키확인">
    
</body>
</html>
 
cs



통상 쿠키 생성시 기한 값을 주지 않으면 브라우저 닫을 때 삭제됩니다.

도움이 되셨으면 공감 부탁드립니다.


+ Recent posts