网上能查到很多类似资料,但完整性不敢恭维。以下是解决方案及要点:
方法一:通过url的方式向flex传参数
第1步:在网页中设置一个链接,如 <a href="test.jsp?p1=test1&p2=test2">测试链接一</a>,其中test.jsp嵌入了flash
第2步:获取参数
var params:Object=URLUtil.parseUrlParameters(ExternalInterface.call("window.location.search.substring", 1));
var param1:String=params["p1"];
parseUrlParameters是一个简单的分析url参数的方法,以下是方法体:
public static function parseUrlParameters(query:String):Object{
var params:Object = {};
if(query) {
var pairs:Array = query.split("&");
for(var i:uint=0; i < pairs.length; i++) {
var pos:int = pairs[i].indexOf("=");
//Alert.show(String(pos));
if(pos != -1) {
var argname:String = pairs[i].substring(0, pos);
var value:String = pairs[i].substring(pos+1);
params[argname] = value;
}
}
}
return params;
}
注:如果window.location.search.substring换成window.location.href.toString 则可获取完整的url
方法二:通过flashvar
第1步:修改index.template.html,在其中加入下面代码红色分,
要点:
1.四处所加内容一般完全相同,因为要考虑到跨浏览器问题,
2、 FlashVars这个名称不能变化
3.FlashVars的值可以是动态获取的,可以是变量,即下例中”233232332232323“可用变量代替或者是“sessionId=233232332232323”整个都可以用变量代替
<body scroll="no">
<script language="JavaScript" type="text/javascript">
<!--
// Version check for the Flash Player that has the ability to start Player Product Install (6.0r65)
var hasProductInstall = DetectFlashVer(6, 0, 65);
// Version check based upon the values defined in globals
var hasRequestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
if ( hasProductInstall && !hasRequestedVersion ) {
// DO NOT MODIFY THE FOLLOWING FOUR LINES
// Location visited after installation is complete if installation is required
var MMPlayerType = (isIE == true) ? "ActiveX" : "PlugIn";
var MMredirectURL = window.location;
document.title = document.title.slice(0, 47) + " - Flash Player Installation";
var MMdoctitle = document.title;
AC_FL_RunContent(
"src", "playerProductInstall",
"FlashVars", "MMredirectURL="+MMredirectURL+'&MMplayerType='+MMPlayerType+'&MMdoctitle='+MMdoctitle+"",
"width", "${width}",
"height", "${height}",
"align", "middle",
"id", "${application}",
"quality", "high",
"bgcolor", "${bgcolor}",
"wmode","transparent", //透明背景
"name", "${application}",
"allowScriptAccess","sameDomain",
"type", "application/x-shockwave-flash",
"pluginspage", "http://www.adobe.com/go/getflashplayer" ,
"FlashVars", "sessionId=233232332232323"
);
} else if (hasRequestedVersion) {
// if we've detected an acceptable version
// embed the Flash Content SWF when all tests are passed
AC_FL_RunContent(
"src", "${swf}",
"width", "${width}",
"height", "${height}",
"align", "middle",
"id", "${application}",
"quality", "high",
"bgcolor", "${bgcolor}",
"wmode","transparent", //透明背景
"name", "${application}",
"allowScriptAccess","sameDomain",
"type", "application/x-shockwave-flash",
"pluginspage", "http://www.adobe.com/go/getflashplayer",
"FlashVars", "sessionId=233232332232323"
);
} else { // flash is too old or we can't detect the plugin
var alternateContent = 'Alternate HTML content should be placed here. '
+ 'This content requires the Adobe Flash Player. '
+ '<a href=http://www.adobe.com/go/getflash/>Get Flash</a>';
document.write(alternateContent); // insert non-flash content
}
// -->
</script>
<noscript>
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
id="${application}" width="${width}" height="${height}"
codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
<param name="movie" value="${swf}.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="${bgcolor}" />
<param name="allowScriptAccess" value="sameDomain" />
<param name="FlashVars" value="sessionId=233232332232323" />
<param name='wmode' value="transparent"><!--//透明背景// -->
<embed src="${swf}.swf" quality="high" bgcolor="${bgcolor}"
width="${width}" height="${height}" name="${application}" align="middle"
play="true"
loop="false"
quality="high"
allowScriptAccess="sameDomain"
type="application/x-shockwave-flash"
FlashVars="sessionId=233232332232323"
pluginspage="http://www.adobe.com/go/getflashplayer" wmode="transparent">
</embed>
</object>
</noscript>
</body>
第2步:在flex中获取参数
用以下几种方法都可以:
要点:
1.parameters后的"sessionId" 必须与上面传过来的参数名字一致
2.parameters得到的传入参数的集合,是个Object,key为传入的参数名,所以可以通过object[key]来获取参数的值
trace(Application.application.parameters.sessionId;//parameters后的sessionId 这个单词必须与上面传过来的参数名字一致
trace(Application.application.parameters["sessionId"]);//也可以获取
trace(this.parameters.sessionId);//也可以获取
trace(this.loaderInfo.parameters.sessionId);//也可以获取