Flex4如何给html 中的swf传参数

来源:互联网 发布:ncbi数据库使用方法 编辑:程序博客网 时间:2024/05/28 09:34
基本上有3种方式: 
1)通过URL查询字符串方式给swf传参数 

URL如下: 
http://localhost:8080/html2Swf/bin-debug/html2Swf.swf?myName=aa&myHometown=aa 

2)通过URL查询字符串方式给swf的wrapper html页面传参数 

URL如下: 
http://localhost:8080/html2Swf/bin-debug/html2Swf.html#firstName=Nick&lastName=Danfffger 

注意,第1中方式中用?, 第2中方式用#, 另外第2种方式里name/value对的分割符可以自定义,代码中解析时作出相应更改即可,详细资料可以参考Flex3帮助文档: 
http://livedocs.adobe.com/flex/3/html/help.html?content=passingarguments_3.html 
http://livedocs.adobe.com/flex/3/html/help.html?content=deep_linking_5.html#245869 

3)在对应的Wrapper html页面中静态配置,有两个地方可以配置: 
位置1 var flashvars = {}; 

案例 var flashvars = {"myName":"Danger","myHometown":"Los%20Angeles_Flashvars"}; 

位置2 
Java代码  收藏代码
  1. swfobject.embedSWF(  
  2.       "${swf}.swf""flashContent",   
  3.       "${width}""${height}",   
  4.       swfVersionStr, xiSwfUrlStr,   
  5.       flashvars, params, attributes);  

案例: 
Java代码  收藏代码
  1. swfobject.embedSWF(  
  2.       "${swf}.swf?myName=aa&myHometown=bb""flashContent",   
  3.       "${width}""${height}",   
  4.       swfVersionStr, xiSwfUrlStr,   
  5.       flashvars, params, attributes);  




综合案例 

按方式1和3传参数,只有上面的TitleWindow里面有值,其中方式3事实上就是不传参数,按方式2传参数,只有下面的TitleWindow里面有参数值. 
Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"   
  3.                xmlns:s="library://ns.adobe.com/flex/spark"   
  4.                xmlns:mx="library://ns.adobe.com/flex/mx"   
  5.                creationComplete="initVars();init(event)" minWidth="955" minHeight="600">  
  6.     <s:layout>  
  7.         <s:VerticalLayout/>  
  8.     </s:layout>  
  9.     <fx:Declarations>  
  10.         <!-- Place non-visual elements (e.g., services, value objects) here -->  
  11.     </fx:Declarations>  
  12.     <fx:Script>  
  13.         <![CDATA[  
  14.         import mx.core.FlexGlobals;  
  15.         import mx.core.mx_internal;  
  16.         use namespace mx_internal;  
  17.         // Declare bindable properties in Application scope.  
  18.         [Bindable]  
  19.         public var myName:String;  
  20.         [Bindable]  
  21.         public var myHometown:String;  
  22.           
  23.         // Assign values to new properties.  
  24.         private function initVars():void {  
  25.             myName = FlexGlobals.topLevelApplication.parameters.myName;  
  26.             myHometown = FlexGlobals.topLevelApplication.parameters.myHometown;  
  27.         }  
  28.           
  29.         import mx.managers.BrowserManager;  
  30.         import mx.managers.IBrowserManager;  
  31.         import mx.utils.URLUtil;  
  32.           
  33.         private var bm:IBrowserManager;  
  34.         [Bindable]  
  35.         private var fName:String;  
  36.         [Bindable]  
  37.         private var lName:String;           
  38.           
  39.         private function init(e:Event):void {  
  40.             bm = BrowserManager.getInstance();  
  41.             bm.init("""Welcome!");  
  42.               
  43.             /* The following code will parse a URL that passes firstName and lastName as 
  44.             query string parameters after the "#" sign; for example: 
  45.             http://www.mydomain.com/MyApp.html#firstName=Nick&lastName=Danger */  
  46.             var o:Object = URLUtil.stringToObject(bm.fragment, "&");  
  47.             fName = o.firstName;  
  48.             lName = o.lastName;  
  49.         }  
  50.   
  51.     ]]>  
  52.     </fx:Script>  
  53.       
  54.     <mx:TitleWindow title="Passing variables to swf directly">  
  55.         <mx:HBox>  
  56.             <mx:Label text="Name: "/>  
  57.             <mx:Label text="{myName}" fontWeight="bold"/>  
  58.         </mx:HBox>  
  59.         <mx:HBox>  
  60.             <mx:Label text="Hometown: "/>  
  61.             <mx:Label text="{myHometown}" fontWeight="bold"/>  
  62.         </mx:HBox>  
  63.     </mx:TitleWindow>  
  64.       
  65.     <mx:TitleWindow title="Passing variables to swf through wrapper html">  
  66.         <mx:Form>  
  67.             <mx:FormItem label="First name:">  
  68.                 <mx:Label id="ti1" text="{fName}"/>  
  69.             </mx:FormItem>  
  70.             <mx:FormItem label="Last name:">  
  71.                 <mx:Label id="ti2" text="{lName}"/>  
  72.             </mx:FormItem>  
  73.         </mx:Form>  
  74.     </mx:TitleWindow>  
  75. </s:Application>  
原创粉丝点击