Flex与JavaScript交互(二)
2008-02-24 09:40
再来看JavaScript调用flex中的函数
JavaScriptConnectFlex.mxml
JavaScriptConnectFlex.html中加入
<button onclick="addMethod()">计算</button>
...
中加入
function addMethod()
{
var s=JavaScriptConnectFlex.addTo100();
alert(s);
}
///////////////
小结
1.ExternalInterface.addCallback("addTo100",add);定义JavaScript可以调用的方法
2.可见JavaScript要调用Flex函数,Flex要声明一下;而Flex调用JavaScript函数则不需要
JavaScriptConnectFlex.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApp()">
<mx:Script>
<![CDATA[
import flash.external.ExternalInterface;
public function add():int
{
var i:int;
var sum:int=0;
for(i=1;i<=100;i++)
{
sum+=i;
}
return sum;
}
public function initApp():void
{
ExternalInterface.addCallback("addTo100",add);
}
]]>
</mx:Script>
</mx:Application>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApp()">
<mx:Script>
<![CDATA[
import flash.external.ExternalInterface;
public function add():int
{
var i:int;
var sum:int=0;
for(i=1;i<=100;i++)
{
sum+=i;
}
return sum;
}
public function initApp():void
{
ExternalInterface.addCallback("addTo100",add);
}
]]>
</mx:Script>
</mx:Application>
JavaScriptConnectFlex.html中加入
<button onclick="addMethod()">计算</button>
...
中加入
function addMethod()
{
var s=JavaScriptConnectFlex.addTo100();
alert(s);
}
///////////////
小结
1.ExternalInterface.addCallback("addTo100",add);定义JavaScript可以调用的方法
2.可见JavaScript要调用Flex函数,Flex要声明一下;而Flex调用JavaScript函数则不需要