最近遇到个问题,开发web项目的时候,可能flex只用来实现项目的部分模块。当flex需要在客户端写入/读取一些状态信息的时候,我们会想到用cookie。flex是不支持cookie的,只有SharedObject这个本地对象。所以解决的办法就有两个:
- flex通过调用js来实现对cookie的操作;
- js通过flex实现对SharedObject的操作;
这两种方法的基础就是实现flex和javascript的交互,自己试着写了个小例子,实现了第一种方法,直接上代码:
Flex_Js_Cookie.js:
- 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:
- <!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:
- <?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下就可以运行了。