Flex 與 javascript通訊

來源:互聯網
上載者:User

在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:  

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.