使用ExternalInterface.call在as中调用js方法:
1.MXML代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx=http://www.adobe.com/2006/mxml
- 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 src="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");
}
转载于:https://blog.51cto.com/aqqle/962676