AS 中的ExternalInterface類 能夠用於和JS的交互javascript
1. AS 調用 JSjava
AS 調用 JS 主要使用 ExternalInterface.call() 方法:app
var result:String = ExternalInterface.call("jsFunc",params); // jsFunc 是 js裏面對應該函數的引用,params是該函數的形參
<script type="text/javascript"> function jsFunc(str){ return("str"); } </script>
2. JS 調用 ASdom
要想在 JS 中調用 AS,須要如今 AS 中調用 ExternalInterface.addCallback() 方法:函數
private function asFunc(s:String):String{ return s; } ExternalInterface.addCallback("asFunc", asFunc); // 「asFunc」 是給外部 JS 調用使用的函數名,asFunc 對應 AS 中該函數的引用
<script type="text/javascript"> function callAs(){ // 先要獲取 <object> 這個dom document.getElementById("player").asfunc(); } </script> <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="100%" height="100%" id="player"> <param name="movie" value="player.swf" /> <param name="quality" value="high" /> <param name="bgcolor" value="#ffffff" /> <param name="allowScriptAccess" value="sameDomain" /> <param name="allowFullScreen" value="true" /> <!--[if !IE]>--> <object type="application/x-shockwave-flash" data="player.swf" width="100%" height="100%"> <param name="quality" value="high" /> <param name="bgcolor" value="#ffffff" /> <param name="allowScriptAccess" value="sameDomain" /> <param name="allowFullScreen" value="true" /> <!--<![endif]--> <!--[if gte IE 6]>--> <p> Either scripts and active content are not permitted to run or Adobe Flash Player version 11.1.0 or greater is not installed. </p> <!--<![endif]--> <a href="http://www.adobe.com/go/getflashplayer"> <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash Player" /> </a> <!--[if !IE]>--> </object> <!--<![endif]--> </object>