ExternalInterface的简单使用方法

ExternalInterface的简单使用方法

使用ExternalInterface调用JavaScript方法-无返回值
flex代码
------------------
<mx:Button id="btn" label="Button" click="hello()"/>
<mx:Script>
 <![CDATA[
  private function hello():void{
         ExternalInterface.call("sayHello");
        }
 ]]>
</mx:Script>

JavaScript代码
---------------------
function sayHello(){
 alert("Hello from JavaScript!");
}

===================================
使用ExternalInterface调用JavaScript方法-传參数有返回值
flex 代码
------------------
<mx:Button id="btn" label="Button" click="hello()"/>
<mx:Script>
 <![CDATA[
  private function hello():void{
         var msg:String=ExternalInterface.call("sayHello","Hello","from JavaScript!");
  Alert.show(msg);
        }
 ]]>
</mx:Script>

JavaScript代码
---------------------
function sayHello(msg,str){
 return msg+"-"+str;
}

===============================================
JavaScript调用flex 方法
flex 代码
------------------
<mx:Button id="btn" label="Button" click="hello()"/>
<mx:Script>
 <![CDATA[
  private function hello():void{
         ExternalInterface.addCallback("getHello",getHello);
        }
 ]]>
</mx:Script>

JavaScript代码
---------------------
function sayHello(){
 var msg=CheckLayout.getHello();
 alert(msg);
}
<body scroll="no">
<input type="button"value="点击我" width="100" onclick="sayHello()"/>
<noscript>
   <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
   id="CheckLayout" width="100%" height="100%"
   codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
   <param name="movie" value="CheckLayout.swf" />
   <param name="quality" value="high" />
   <param name="bgcolor" value="#869ca7" />
   <param name="allowScriptAccess" value="sameDomain" />
   <embed src="CheckLayout.swf" quality="high" bgcolor="#869ca7"
    width="100%" height="100%" name="CheckLayout" align="middle"
    play="true"
    loop="false"
    quality="high"
    allowScriptAccess="sameDomain"
    type="application/x-shockwave-flash"
    pluginspage="http://www.adobe.com/go/getflashplayer">
   </embed>
 </object>
</noscript>
</body>

上一篇:IOS开发之──应用之间调用(2)


下一篇:Java Sevlet 之 过滤器 Filter