基本上有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
swfobject.embedSWF(
"${swf}.swf", "flashContent",
"${width}", "${height}",
swfVersionStr, xiSwfUrlStr,
flashvars, params, attributes);
案例:
swfobject.embedSWF(
"${swf}.swf?myName=aa&myHometown=bb", "flashContent",
"${width}", "${height}",
swfVersionStr, xiSwfUrlStr,
flashvars, params, attributes);
综合案例
按方式1和3传参数,只有上面的TitleWindow里面有值,其中方式3事实上就是不传参数,按方式2传参数,只有下面的TitleWindow里面有参数值.
<?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"
creationComplete="initVars();init(event)" minWidth="955" minHeight="600">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.core.FlexGlobals;
import mx.core.mx_internal;
use namespace mx_internal;
// Declare bindable properties in Application scope.
[Bindable]
public var myName:String;
[Bindable]
public var myHometown:String;
// Assign values to new properties.
private function initVars():void {
myName = FlexGlobals.topLevelApplication.parameters.myName;
myHometown = FlexGlobals.topLevelApplication.parameters.myHometown;
}
import mx.managers.BrowserManager;
import mx.managers.IBrowserManager;
import mx.utils.URLUtil;
private var bm:IBrowserManager;
[Bindable]
private var fName:String;
[Bindable]
private var lName:String;
private function init(e:Event):void {
bm = BrowserManager.getInstance();
bm.init("", "Welcome!");
/* The following code will parse a URL that passes firstName and lastName as
query string parameters after the "#" sign; for example:
http://www.mydomain.com/MyApp.html#firstName=Nick&lastName=Danger */
var o:Object = URLUtil.stringToObject(bm.fragment, "&");
fName = o.firstName;
lName = o.lastName;
}
]]>
</fx:Script>
<mx:TitleWindow title="Passing variables to swf directly">
<mx:HBox>
<mx:Label text="Name: "/>
<mx:Label text="{myName}" fontWeight="bold"/>
</mx:HBox>
<mx:HBox>
<mx:Label text="Hometown: "/>
<mx:Label text="{myHometown}" fontWeight="bold"/>
</mx:HBox>
</mx:TitleWindow>
<mx:TitleWindow title="Passing variables to swf through wrapper html">
<mx:Form>
<mx:FormItem label="First name:">
<mx:Label id="ti1" text="{fName}"/>
</mx:FormItem>
<mx:FormItem label="Last name:">
<mx:Label id="ti2" text="{lName}"/>
</mx:FormItem>
</mx:Form>
</mx:TitleWindow>
</s:Application>