flex 学习笔记 ExternalInterface
使用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 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");
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章