티스토리 뷰

[ CookieUtil.as ]

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
78
79
package has3
{
    import flash.external.ExternalInterface;
 
    public class CookieUtil
    {
        public function CookieUtil()
        {
        }
        
        private static const FUNCTION_SETCOOKIE:String = 
            "document.insertScript = function ()" +
            "{ " +
            "if (document.snw_setCookie==null)" +
            "{" +
            "snw_setCookie = function (name, value, days)" +
            "{" +
            "if (days) {"+
            "var date = new Date();"+
            "date.setTime(date.getTime()+(days*24*60*60*1000));"+
            "var expires = '; expires='+date.toGMTString();"+
            "}" +
            "else var expires = '';"+
            "document.cookie = name+'='+value+expires+'; path=/';" +
            "}" +
            "}" +
            "}";
        
        private static const FUNCTION_GETCOOKIE:String = 
            "document.insertScript = function ()" +
            "{ " +
            "if (document.snw_getCookie==null)" +
            "{" +
            "snw_getCookie = function (name)" +
            "{" +
            "var nameEQ = name + '=';"+
            "var ca = document.cookie.split(';');"+
            "for(var i=0;i < ca.length;i++) {"+
            "var c = ca[i];"+
            "while (c.charAt(0)==' ') c = c.substring(1,c.length);"+
            "if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);"+
            "}"+
            "return null;" +
            "}" +
            "}" +
            "}";
        
        
        private static var INITIALIZED:Boolean = false;
        
        private static function init():void{
            ExternalInterface.call(FUNCTION_GETCOOKIE);
            ExternalInterface.call(FUNCTION_SETCOOKIE);
            INITIALIZED = true;
        }
        
        public static function setCookie(name:String, value:Object, days:int):void{
            if(!INITIALIZED)
                init();
            
            ExternalInterface.call("snw_setCookie"name, value, days);
        }
        
        public static function getCookie(name:String):Object{
            if(!INITIALIZED)
                init();
            
            return ExternalInterface.call("snw_getCookie"name);
        }
        
        public static function deleteCookie(name:String):void{
            if(!INITIALIZED)
                init();
            
            ExternalInterface.call("snw_setCookie"name""-1);
        }
        
    }
}
cs

[출처] http://myflex.wordpress.com/2008/11/12/actionscript-cookie-util/

사용방법

CookieUtil.set("쿠키명","저장내용",저장기간);

CookieUitl.deleteCookie("쿠키명");

CookieUtil.getCookie("쿠키명");

[ test.mxml ] CookieUtil.cs를 사용한 예

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
<?xml version="1.0" encoding="utf-8"?>
<!--
쿠키사용예
-->
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
<fx:Script>
<![CDATA[
    import flash.external.ExternalInterface;
    import has3.CookieUtil;
    /**
     * 쿠키저장
     * */
    private function fn_CookieIN():void
    {
        //(쿠키명,쿠키저장내용,저장기간day)
        CookieUtil.setCookie("hks003",txtIn.text,1);
    }
    /**
     * 쿠키값 읽어오기
     * */
    private function fn_CookieOUT():void
    {
        labOut.text = CookieUtil.getCookie("hks003").toString();        
    }
    /**
     * 쿠키값 삭제
     * */
    private function fn_CookieDEL():void
    {
        CookieUtil.deleteCookie("hks003");
    }
]]>
</fx:Script>
    <s:Button x="108" y="172" label="쿠기저장" click="fn_CookieIN()"/>
    <s:Button x="578" y="172" label="쿠키불러오기"  click="fn_CookieOUT()"/>
    <s:Label id="labOut" x="578" y="213" width="273" text="Label"/>
    <s:TextInput id="txtIn" x="104" y="202"/>
    <s:Button x="385" y="132" label="쿠키지워" click="fn_CookieDEL()"/>
</s:Application>
 
cs