flex生成的flash在与js交互
1,在加载flash的html页面必须引进 js文件
2,在flex中调用js方法
var str:String=ExternalInterface.call("callJS");
"callJS"是js文件的方法名。str是js中方法的放回值,如果没有返回值的话可以不写。
var str:String=ExternalInterface.call("callJS","params1");
params1是参数名称
2,js调用flex中的方法
a,如果要js调用flex那么必须在flex加载完毕才能执行
b
例子
1,在加载flash的html页面必须引进 js文件
2,在flex中调用js方法
var str:String=ExternalInterface.call("callJS");
"callJS"是js文件的方法名。str是js中方法的放回值,如果没有返回值的话可以不写。
var str:String=ExternalInterface.call("callJS","params1");
params1是参数名称
2,js调用flex中的方法
a,如果要js调用flex那么必须在flex加载完毕才能执行
b
ExternalInterface.addCallback("jsfunctionName",flexHelloWorld);
public function flexword(str1:String,str2:String):void{
Alert.show("js调用flex成功==="+str1+str2);
}
jsfunctionName 是在js中调用flex的方法,flexHelloWorld 回滚的方法,也就是当js调用了jsfunctionName以后执行要执行的方法,str1:String,str2:String是js调用
jsfunctionName 传入的参数
js方法如下:
function jspHello(str){
//etmmp 是html加载swf的id
var fl = document.getElementById("etmmp");
alert(fl);
var x= fl.flexHelloWorld("Hello", "world");
}
例子
<?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" minWidth="955" minHeight="600" creationComplete="application1_creationCompleteHandler(event)">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.FlexEvent;
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
ExternalInterface.addCallback("flexHelloWorld",flexword);
}
public function flexword(str1:String,str2:String):void{
Alert.show("js调用flex成功==="+str1+str2);
}
public function jspHello():void {
var s:String = ExternalInterface.call("jspHello", "Flex");
Alert.show(s);
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<mx:Button x="52" y="58" label="call sssss javascript" click="jspHello()"/>
</s:Application>
function jspHello(str){
var fl = document.getElementById("etmmp");
alert(fl);
var x= fl.flexHelloWorld("Hello", "world");
}