电脑技术学习

JavaScript与ActionScript通讯(ExternalInterface类)

dn001

需要引入import flash.external.ExternalInterface;

一、ActionScript调用JavaScript的方法

这里要用到ExternalInterface类的call方法

ExternalInterface.call(functionName:String,...arguments):*

fuctionName:要调用的JavaScript函数名

arguments:参数,可选

1.不带参数的情况

JavaScript:function Show() {alert("I am a func!");}

ActionScript://直接用一条语句调用ExternalInterface.call("Show");

2.带参数的情况

JavaScript:function Show(message) {;alert(message);}

ActionScript:ExternalInterface.call("Show","I am a message from AS");;;另外,也可以用getURL方法来调用

getURL("javascript:show('i am a message from as)","_self");

二、JavaScript调用ActionScript的方法

这要用到ExternalInterface类的addCallback方法

ExternalInterface.addCallback( functionName:String, closure:Function):void;

functionName:要注册的函数名

closure:对应的执行函数

ActionScript:ExternalInterface.addCallback("Show",OnShow);private function OnShow(message:String):string{return message;}

JavaScript:function thisMovie(movieName){;if(navigator.appName.indexOf("Microsoft") != -1){;;;;;return window[movieName];;}else{ return document[movieName];;}} function CallAS(; ) {; thisMovie("ViewLesson").Show("i am a message from js");//ViewLesson是flash媒体的ID}

HTML:<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"id="ViewLesson" width="100%" height="100%"codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab"><param name="movie" value="ViewLesson.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#869ca7" /><param name="allowScriptAccess" value="sameDomain" /><embed src="ViewLesson.swf" quality="high" bgcolor="#869ca7";width="100%" height="100%" name="ViewLesson" align="middle";play="true";loop="false";quality="high";allowScriptAccess="sameDomain";type="application/x-shockwave-flash";pluginspage="http://www.adobe.com/go/getflashplayer"></embed></object>