這也是js方法"eval"的一種應用。函數
在flash中生成js的方法,而後經過ExternalInterface調用。spa
經過這種機制,能夠在Flash中實現整個頁面刷新,以及在Flash中獲取所在網頁的連接等功能。code
代碼很簡單,我就不直接說了,貼出來,你們能夠直接看效果 xml
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" fontFamily="Times New Roman" fontSize="12" creationComplete="init()"> <mx:Script> <![CDATA[ import mx.controls.Alert; import flash.external.ExternalInterface; private function init():void { //須要在js生成的代碼 var str:String = "function testMethod(str)"; str = str + "{"; str = str + "alert('Flash傳來:' + str);"; str = str + "return 'js返回:' + str;"; str = str + "}"; //利用eval函數生成 ExternalInterface.call("eval", str); } private function buttonClick():void { //調用剛剛生成的方法 var str:String = ExternalInterface.call("testMethod", valueText.text); Alert.show(str); } private function fulshHtmlEvent():void { //刷新頁面 ExternalInterface.call("eval", "location.reload();"); } ]]> </mx:Script> <mx:Button label="提交" id="subimtBtn" click="buttonClick()"/> <mx:TextInput width="122" id="valueText"/> <mx:Label text="Flash數據:"/> <mx:Button label="刷新頁面" id="flushBtn" click="fulshHtmlEvent()"/> </mx:Application>
實現Flash調用JS方法,有如下三點須要注意: ip
必須在Flash代碼中導入ExternalInterface,即添加以下代碼utf-8
import flash.external.ExternalInterfaceflash
引用Flash的object標籤必須有id屬性。it
object標籤中必須傳遞allowScriptAccess參數,同時object中的embed標籤也要加上這個屬性。io
allowScriptAccess的值能夠爲always, sameDomain, never。function