flex 中使用Cookie

http://coenraets.com/apps/cv.jsp?descriptor=cookie/source.xml

Cookieapp.jsp 文件


<%@ taglib uri="FlexTagLib" prefix="mm"%>
<SCRIPT LANGUAGE="JavaScript">
<!--
function setCookie(userId) {
    document.cookie = "userId="+escape(userId);
}

function getCookie() {
    var cookieStr = document.cookie;
    var index = cookieStr.indexOf("userId=");
    if (index == -1) return null;
    index = cookieStr.indexOf("=", index) + 1;
    var endstr = cookieStr.indexOf(";", index);
    if (endstr == -1) endstr = cookieStr.length;
    window.document.flexApp.SetVariable("userIdJS", unescape(cookieStr.substring(index, endstr)));
}
-->
</SCRIPT>
<mm:mxml name="flexApp" source="cookie.mxml"/>


cookie.mxml

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" width="300" height="400">

    <mx:Script>
        function setCookie() {
            srv.send({action: 'set', userId: userId.text});
        }

        function getCookie() {
            srv.send({action: 'get'});
        }

        function setCookieJS() {
            getURL("javascript:setCookie('"+userId.text+"')");
        }

        function getCookieJS() {
            getURL("javascript:getCookie()");
        }

        // callback function invoked by the getCookie() JavaScript function in cookieapp.jsp
        function set userIdJS(userIdJS:String) {
            userId.text=userIdJS;
        }
    </mx:Script>

    <mx:HTTPService id="srv" url="cookie.jsp" useProxy="false" method="POST"
        result="userId.text=srv.result.userId"/>

    <mx:Label text="UserId:"/>
    <mx:TextInput id="userId"/>
    <mx:Button label="Set Cookie Value Using JSP" width="220" click="setCookie()"/>
    <mx:Button label="Set Cookie Value Using JavaScript" click="setCookieJS()"/>
    <mx:Button label="Get Cookie Value Using JSP" width="220" click="getCookie()"/>
    <mx:Button label="Get Cookie Value Using JavaScript" width="220" click="getCookieJS()"/>

</mx:Application>

cookie.jsp

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" width="300" height="400">

    <mx:Script>
        function setCookie() {
            srv.send({action: 'set', userId: userId.text});
        }

        function getCookie() {
            srv.send({action: 'get'});
        }

        function setCookieJS() {
            getURL("javascript:setCookie('"+userId.text+"')");
        }

        function getCookieJS() {
            getURL("javascript:getCookie()");
        }

        // callback function invoked by the getCookie() JavaScript function in cookieapp.jsp
        function set userIdJS(userIdJS:String) {
            userId.text=userIdJS;
        }
    </mx:Script>

    <mx:HTTPService id="srv" url="cookie.jsp" useProxy="false" method="POST"
        result="userId.text=srv.result.userId"/>

    <mx:Label text="UserId:"/>
    <mx:TextInput id="userId"/>
    <mx:Button label="Set Cookie Value Using JSP" width="220" click="setCookie()"/>
    <mx:Button label="Set Cookie Value Using JavaScript" click="setCookieJS()"/>
    <mx:Button label="Get Cookie Value Using JSP" width="220" click="getCookie()"/>
    <mx:Button label="Get Cookie Value Using JavaScript" width="220" click="getCookieJS()"/>

</mx:Application>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值