JavaScript和ActionScript的交互实现代码
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import flash.external.ExternalInterface;
public function invokeJavaScript():void
{
lblResult.text=ExternalInterface.call("returnTheName",txtName.text);
}
]]>
</mx:Script>
<mx:Panel width="800" height="600" title="ActionScript调用JavaScript" verticalCenter="0" horizontalCenter="0">
<mx:TextInput id="txtName" text="[请输入名字]"/>
<mx:Button id="btnConnect" label="确定" click="invokeJavaScript()" x="229" y="204"/>
<mx:Label id="lblResult" text="test" x="205" y="99" width="150" height="80"/>
</mx:Panel>
</mx:Application>
在html页面里javascript里面加上方法function returnTheName(a){return "success! "+a};
以上是as调用js里的方法
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()" layout="absolute">
<mx:Script>
<![CDATA[
import flash.external.ExternalInterface;
private function init():void
{
ExternalInterface.addCallback("addTo100",add);//声明可以被调用的方法
}
private function add():int
{
var i:int;
var sum:int=0;
for(i=1 ; i<100 ; i++)
{
sum+= i;
}
return sum;
}
]]>
</mx:Script>
</mx:Application>
在html页里面加上<button onclick="addMethod()">点击调用</button>
js文件里加上方法function addMethod{var s= JavaScriptCallActionScript.addTo100();alert(s);}