flex 學習筆記 ExternalInterface

TigerJin發表於2021-09-09

使用ExternalInterface.call在as中呼叫js方法:

1.MXML程式碼如下:

<?xml version="1.0" encoding="utf-8"?>   <mx:Application xmlns:mx=  layout="vertical" verticalAlign="middle" backgroundColor="white">  <mx:Script>  <![CDATA[   private function callJavaScript():void   {   ExternalInterface.call("sayHelloWorld");   }   ]]>  </mx:Script>  <mx:Button label="Say 'Hello World'" click="callJavaScript();" />  </mx:Application>

2.js程式碼:

<script language="JavaScript" type="text/javascript">  function sayHelloWorld()  {   alert("Hello World, from JavaScript");   }   </script>

或者,可以不用直接在HTML頁中寫JavaScript程式碼,而是以外部JS檔案匯入的方式:

/src/scripts.js

function sayHelloWorld() {
alert("Hello World, from JavaScript");
}

------------------------
/html-template/index.template.html

<script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="scripts.js" language="JavaScript"></script>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

如果想向JavaScript函式中傳參,方法如下:

// JavaScript
function sayString(str) {
alert(str);
}
// ActionScript 3.0
private function callJavaScript():void {
ExternalInterface.call("sayString", "Hello World, from ActionScript");
}
 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/2144/viewspace-2815537/,如需轉載,請註明出處,否則將追究法律責任。

相關文章