在Flex中有这么一个类:ExternalInterface.在这个类中它给我们:call和addCallback
Flex中As调用Js的方法是:
1、导入包 (import flash.external.ExternalInterface;)
2、使用ExternalInterface.call("Js函数名称",参数)进行调用,其返回的值就是Js函数所返回的值
Js调用As的方法是:
1、导入包 (import flash.external.ExternalInterface;)
2、在initApp中使用ExternalInterface.addCallback("用于Js调用的函数名",As中的函数名)进行注册下
3、js中 就可以用document.getElementById("Flas在Html中的ID").注册时设置的函数名(参数)进行调用.
简单示例:
1: <script language="JavaScript" type="text/javascript">
2: function callFlexFunction() {
3: var x = document.getElementById("JavaScriptTest").flexTest("string from js.");
4: alert(x);
5: }
6:
7: function jsTest(param) {
8: alert(param);
9: return "Return from js:" + param;
10: }
11: </script>
1: <?xml version="1.0" encoding="utf-8"?>
2: <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="intiApp()"
3: width="400" height="200">
4: <mx:Script>
5: <![CDATA[
6: import mx.controls.Alert;
7:
8: public function flexTest(param:String):String
9: {
10: Alert.show("Input param:"+param);
11: return "Return from Flex:"+param;
12: }
13:
14: public function intiApp():void
15: {
16: ExternalInterface.addCallback("flexTest", flexTest);
17: }
18:
19: public function callJS():void {
20: var s:String = ExternalInterface.call("jsTest", "Flex");
21: Alert.show(s);
22: }
23: ]]>
24: </mx:Script>
25:
26: <mx:Button x="52" y="58" label="call javascript" click="callJS()"/>
27: </mx:Application>
28: