Flex开发中如何给SWF传递外部提供的参数
在SWF中嵌入另外一个SWF程序时,有时会碰到如何将外部的业务参数传递给内部的SWF文件,这时候就可以利用flashvars,具体可以将参数配置到对应SWF文件的html文件中,具体配置如下。
1. Test.html中配置参数
.....
<head>
<title>${title}</title>
<meta name="google" value="notranslate" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- Include CSS to eliminate any default margins/padding and set the height of the html element and
the body element to 100%, because Firefox, or any Gecko based browser, interprets percentage as
the percentage of the height of its parent container, which has to be set explicitly. Fix for
Firefox 3.6 focus border issues. Initially, don't display flashContent div so it won't show
if JavaScript disabled.
-->
<style type="text/css" media="screen">
html, body { height:100%; }
body { margin:0; padding:0; overflow:auto; text-align:center;
background-color: ${bgcolor}; }
object:focus { outline:none; }
#flashContent { display:none; }
</style>
<!-- Enable Browser History by replacing useBrowserHistory tokens with two hyphens -->
<!-- BEGIN Browser History required section ${useBrowserHistory}>
<link rel="stylesheet" type="text/css" href="history/history.css" />
<script type="text/javascript" src="history/history.js"></script>
<!${useBrowserHistory} END Browser History required section -->
<script type="text/javascript" src="swfobject.js"></script>
<script type="text/javascript">
// For version detection, set to min. required Flash Player version, or 0 (or 0.0.0), for no version detection.
var swfVersionStr = "${version_major}.${version_minor}.${version_revision}";
// To use express install, set to playerProductInstall.swf, otherwise the empty string.
var xiSwfUrlStr = "${expressInstallSwf}";
var flashvars = {};
//****** begin 在此处配置访问后台服务地址 *********
flashvars.url=http://127.0.0.1:8080;
flashvars.contextRoot="test";
//****** end 在此处配置访问后台服务地址 *********
var params = {};
params.quality = "high";
params.bgcolor = "${bgcolor}";
params.allowscriptaccess = "sameDomain";
params.allowfullscreen = "true";
var attributes = {};
attributes.id = "${application}";
attributes.name = "${application}";
attributes.align = "middle";
swfobject.embedSWF(
"${swf}.swf", "flashContent",
"${width}", "${height}",
swfVersionStr, xiSwfUrlStr,
flashvars, params, attributes);
// JavaScript enabled so display the flashContent div in case it is not replaced with a swf object.
swfobject.createCSS("#flashContent", "display:block;text-align:left;");
</script>
</head>
.....
2.Test.mxml中获取外部配置的参数
<s:AbstractApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:fc="http://ns.adobe.com/flashcatalyst/2009" width="100%" height="100%"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:controls="com.sgcc.sgtms.ilog.ui.controls.*"
creationComplete="init()">
<fx:Script>
<![CDATA[
private function init():void
{
//Flex4以上采用FlexGlobals.topLevelApplication获取.html中的参数, Flex3采用Application.application
var urlValue:String = FlexGlobals.topLevelApplication.parameters.url;
var contextRootValue:String = FlexGlobals.topLevelApplication.parameters.contextRoot;
//提供后去访问该SWF文件的路径
var swfURL:String = this.loaderInfo.url;
............
}
]]>
</fx:Script>
............
</s:AbstractApplication >
其它代码省略。只提供思路:该配置步骤只适合单个Application编译生成的SWF使用。
在实际开发环境中,可能还会遇到将Test.SWF文件嵌入其它SWF文件中,那么在以上Test.html中的参数配置就需要转移到对应的父SWF文件的html中。