Flex通过javascript读写cookie

当flex需要在客户端写入/读取一些状态信息的时候,我们会想到用cookie。flex是不支持cookie的,只有SharedObject这个本地对象。所以解决的办法就有两个:

flex通过调用js来实现对cookie的操作;
js通过flex实现对SharedObject的操作;
这两种方法的基础就是实现flex和javascript的交互,自己试着写了个小例子,实现了第一种方法,直接上代码:

Flex_Js_Cookie.js:

 view plaincopy to clipboardprint?
function SetCookie(name,value)  
{  
    document.cookie = name+"="+escape(value);  
};  
function GetCookie(name)  
{  
    var arr = document.cookie.match(new RegExp("(^|)"+name+"=([^;]*)(;|$)"));  
    alert(arr.length);  
    if(arr != null)  
    {  
        return unescape(arr[2]);  
    }  

function SetCookie(name,value)
{
 document.cookie = name+"="+escape(value);
};
function GetCookie(name)
{
 var arr = document.cookie.match(new RegExp("(^|)"+name+"=([^;]*)(;|$)"));
 alert(arr.length);
 if(arr != null)
 {
  return unescape(arr[2]);
 }
}

Flex_Js_Cookie.html:

view plaincopy to clipboardprint?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<head> 
<title></title> 
<script src="swfobject.js" type="text/javascript"></script> 
<script src="Flex_Js_Cookie.js" type="text/javascript"></script> 
<script language=javascript> 
var flashvars = {};  
        var params = {  
            menu: "false",  
            scale: "noScale",  
            allowFullscreen: "true",  
            allowScriptAccess: "always",  
            bgcolor: "#FFFFFF"  
        };  
        var attributes = {id:"swfplayer"};  
        swfobject.embedSWF("Flex_Js_Cookie.swf", "swfplayer", "500", "350", "9.0.0", "expressInstall.swf", flashvars, params, attributes);  
</script> 
</head> 
<body> 
<div id="swfplayer"></div> 
</body> 
</html> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<head>
<title></title>
<script src="swfobject.js" type="text/javascript"></script>
<script src="Flex_Js_Cookie.js" type="text/javascript"></script>
<script language=javascript>
var flashvars = {};
  var params = {
   menu: "false",
   scale: "noScale",
   allowFullscreen: "true",
   allowScriptAccess: "always",
   bgcolor: "#FFFFFF"
  };
  var attributes = {id:"swfplayer"};
  swfobject.embedSWF("Flex_Js_Cookie.swf", "swfplayer", "500", "350", "9.0.0", "expressInstall.swf", flashvars, params, attributes);
</script>
</head>
<body>
<div id="swfplayer"></div>
</body>
</html>
 

Flex_Js_Cookie.mxml:

view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> 
    <mx:Script> 
        <![CDATA[ 
            import flash.external.*; 
            import mx.controls.Alert; 
             
            public function GetCookie():void 
            { 
                var jsFunction:String = "GetCookie"; 
                var key:String = txt_key.text; 
                if(ExternalInterface.available) 
                { 
                    var value:String = ExternalInterface.call(jsFunction,key); 
                    txt_value.text = value; 
                } 
            } 
             
            public function SetCookie():void 
            { 
                var jsFunction:String = "SetCookie"; 
                var key:String = txt_key_set.text; 
                var value:String = txt_value_set.text; 
                if(ExternalInterface.available) 
                { 
                    ExternalInterface.call(jsFunction,key,value); 
                } 
            } 
        ]]> 
    </mx:Script> 
    <mx:Label x="51" y="39" text="cookie名:" fontSize="12"/> 
    <mx:Label x="51" y="86" text="cookie值:" fontSize="12"/> 
    <mx:TextInput x="122" y="39" id="txt_key"/> 
    <mx:Button x="301" y="84" label="确定" fontSize="12" click="GetCookie()"/> 
    <mx:TextInput x="122" y="86" id="txt_value" enabled="false"/> 
    <mx:Label x="51" y="183" text="cookie名:" fontSize="12"/> 
    <mx:Label x="51" y="226" text="cookie值:" fontSize="12"/> 
    <mx:TextInput x="122" y="183" id="txt_key_set"/> 
    <mx:TextInput x="122" y="226" id="txt_value_set"/> 
    <mx:Button x="301" y="226" label="确定" fontSize="12" click="SetCookie()"/> 
    <mx:Label x="51" y="155" text="设置cookie" fontSize="12" color="#C42A2A"/> 
    <mx:Label x="51" y="11" text="取得cookie" fontSize="12" color="#C42A2A"/> 
      
</mx:Application> 
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
 <mx:Script>
  <![CDATA[
   import flash.external.*;
   import mx.controls.Alert;
   
   public function GetCookie():void
   {
    var jsFunction:String = "GetCookie";
    var key:String = txt_key.text;
    if(ExternalInterface.available)
    {
     var value:String = ExternalInterface.call(jsFunction,key);
     txt_value.text = value;
    }
   }
   
   public function SetCookie():void
   {
    var jsFunction:String = "SetCookie";
    var key:String = txt_key_set.text;
    var value:String = txt_value_set.text;
    if(ExternalInterface.available)
    {
     ExternalInterface.call(jsFunction,key,value);
    }
   }
  ]]>
 </mx:Script>
 <mx:Label x="51" y="39" text="cookie名:" fontSize="12"/>
 <mx:Label x="51" y="86" text="cookie值:" fontSize="12"/>
 <mx:TextInput x="122" y="39" id="txt_key"/>
 <mx:Button x="301" y="84" label="确定" fontSize="12" click="GetCookie()"/>
 <mx:TextInput x="122" y="86" id="txt_value" enabled="false"/>
 <mx:Label x="51" y="183" text="cookie名:" fontSize="12"/>
 <mx:Label x="51" y="226" text="cookie值:" fontSize="12"/>
 <mx:TextInput x="122" y="183" id="txt_key_set"/>
 <mx:TextInput x="122" y="226" id="txt_value_set"/>
 <mx:Button x="301" y="226" label="确定" fontSize="12" click="SetCookie()"/>
 <mx:Label x="51" y="155" text="设置cookie" fontSize="12" color="#C42A2A"/>
 <mx:Label x="51" y="11" text="取得cookie" fontSize="12" color="#C42A2A"/>
 
</mx:Application>
 

通过代码,会发现在html文件里面使用了swfobject.js这个文件,这是一个第三方发布的工具包,方便我们在页面中使用flash,下载地址是:http://code.google.com/p/swfobject/downloads/list。只要里面的js文件。

mxml文件要预先编译成swf文件。然后一起部署到tomcat下就可以运行了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值