JS与AS通信-转

核心

ExternalInterface.addCallback("js_func",as_func);//将AS函数(as_func)暴露出去,使用的时候通过 Flash对象.js_func进行访问。

ExternalInterface.call("js_func"); //调用Flash对象所在页面(html)中的JS方法(js_func)

示例:

AS

 1 <?xml version="1.0" encoding="utf-8"?>
 2 
 3 <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
 4 
 5              xmlns:s="library://ns.adobe.com/flex/spark"
 6 
 7              xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="makeOut()">
 8 
 9 <fx:Script>
10 
11         <![CDATA[
12 
13 public function makeOut():void{
14 
15                 ExternalInterface.addCallback("asDo",asDo);
16 
17                 ExternalInterface.call("jsDo");
18 
19             }
20 
21             public function asDo(){
22 
23                 Alert.show("AS 干活啦!");
24 
25 }
26 
27 ]]>
28 
29 </fx:Script>
30 
31 </s:Application> 

JS

 1 <script type="text/javascript">
 2 
 3 function jsDo(){
 4 
 5 alert("JS先干活");
 6 
 7 var obj=getFlashObject();
 8 
 9 obj. asDo();
10 
11 }
12 
13 </script>
原文地址:https://www.cnblogs.com/LLLoveLL/p/3333173.html