程序运行环境:tomcat5.0+flex1.5
原码
E:\Tomcat 5.0\webapps\myTry\twoFlex\Flex_JSCommunication2.jsp:
<%@ taglib uri="FlexTagLib" prefix="mm" %>
<HTML>
<HEAD>
<meta http-equiv=Content-Type content="text/html; charset=ISO-8859-1">
<TITLE>Flex-JS Communication Demo</TITLE>
<script language="javascript" src="../com/jinnaluo/until/flashJSComm.js"></script>
</HEAD>
<BODY bgcolor="#FFFFFF">
<mm:mxml id="movie1" source="movie1.mxml" width="300" height="300"/>
<BR>
<mm:mxml id="movie2" source="movie2.mxml" width="300" height="300"/>
</BODY>
</HTML>
----------------------------
E:\Tomcat 5.0\webapps\myTry\twoFlex\movie1.mxml:
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" xmlns="*" width="500" height="500" backgroundColor="#FFFFFF" creationComplete="initIt()">
<mx:Script source="../com/jinnaluo/until/flashJSComm.as"/>
<mx:Script>
<![CDATA[
var dpArr;
function initIt(){
dpArr="1235654675646513212137676456546513135464".split("");
}
public function setNum(param){
param="["+param+"]";
invokeOthFlex("movie2","setNum",param);
}
]]>
</mx:Script>
<mx

ile width="250" height="100%" direction="horizontal" horizontalGap="2">
<mx:Repeater dataProvider="{dpArr}" id="rpMain">
<mx:Button label="{rpMain.currentItem}" width="30" height="30" click="setNum(event.target.label)"/>
</mx:Repeater>
</mx

ile>
</mx:Application>
---------------------------------------------------
E:\Tomcat 5.0\webapps\myTry\twoFlex\movie2.mxml:
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" xmlns="*" width="600" height="600" backgroundColor="#FFFFFF"
creationComplete="initIt()">
<mx:Script source="../com/jinnaluo/until/flashJSComm.as"/>
<mx:Script>
<![CDATA[
private var dpArr;
private var visibleNum=-1;
function initIt(){
dpArr="1111111111111111111111111111111111111111111111111111".split("");
}
public function setNum(param){
var obj=nextVisible();
obj.label=param;
obj.visible=true;
}
private function nextVisible(){
visibleNum++;
return tl1.getChildAt(visibleNum);
}
]]>
</mx:Script>
<mx

ile width="250" height="100%" direction="horizontal" horizontalGap="2" id="tl1">
<mx:Repeater dataProvider="{dpArr}" id="rpMain">
<mx:Button label="{rpMain.currentItem}" width="30" height="30" visible="false"/>
</mx:Repeater>
</mx

ile>
</mx:Application>
-------------------------------------------------
E:\Tomcat 5.0\webapps\myTry\com\jinnaluo\until\flashJSComm.as:
//the name of the function to be called.
var functionName:String;
//the params delimited by paramDelimiter
var functionParams:String;
//param delemiter, kind of unique string
var paramDelimiter = "-->$$###$$##$$<--";
public function set commitJSCall(flag:Boolean):Void
{
//mx.controls.Alert.show("commit a JS call:"+functionName+newline+functionParams);
var func:Function = this[functionName];
func.apply(this, functionParams.split(paramDelimiter));
getURL("javascript:bFunctionCallFinished=true;void(0)");
}
public function invokeOthFlex(movieID,funcName,param):Void
{
getURL("javascript:callFlashFunction("+movieID+","+funcName+","+param+");void(0)");
}
-----------------------------------------------------
E:\Tomcat 5.0\webapps\myTry\com\jinnaluo\until\flashJSComm.js:
//this function return to Flash ActiveX Object or Plugin depending upon browser
function thisMovie(movieName) {
if (navigator.appName.indexOf ("Microsoft") !=-1) {
return window[movieName];
} else {
return window.document[movieName];
}
}
/**###############################################**/
//success flag, set by flash/flex app
var bFunctionCallFinished = true;
//param delimiter, kind of unique string
var paramDelimiter = "-->$$###$$##$$<--";
//this function would call a flash/flex function.
function callFlashFunction(movieName,functionName, paramsArray){
if(bFunctionCallFinished) {
var flashObject = thisMovie(movieName);
bFunctionCallFinished = false;
flashObject.SetVariable("functionName", functionName);
flashObject.SetVariable("functionParams", paramsArray.join(paramDelimiter));
flashObject.SetVariable("commitJSCall", true);
}
else{
//if previous function call is still being run, you can use setTimeOut etc to call it little later...
}
}
--------------------------------------------------------------