AS与JS通信,官方帮助文档已经说的比较明白了。做什么事,都要得准备好。想要JS调用AS里边的函数,AS这边必须满足两个条件。第一,swf必须已经加载完毕了。第二,AS这边必须注册为可被容器调用。
if(ExternalInterface.available) { ExternalInterface.addCallback("receiveMes",asReceiveMes); ExternalInterface.addCallback("backMes",jsBackMes); }
if 语句,判断此播放器是否位于提供外部接口的容器中。 如果外部接口可用,则此属性为 true
;否则,为 false
。addCallback方法就是注册该swf可被容器调用。它有两个参数,都是函数名。前边一个函数名,用引号引起来,表示JS那边对应AS这边的
函数名。后边一个函数名,是AS这边响应的函数名。这两个函数名,可以相同,也可以不同。看个人理解需要。
AS这边主动调用JS里边的函数,用call方法。call方法第一个参数为JS那边的函数名,是必须要的。你也可以选择传一个或多个参数过去。String,Number,int,Array,Boolean,Object等数据类型都可以互相传递。
ExternalInterface.call("sendMes",_inputTxt.text);
主要是js那边的设置。其实就是包含js的Html代码。这里主要有两个标签要用到。一个是object,一个是embed。在object标签里边有两个属性需要注意。allowScriptAccess属性默认是sameDomain。还有一个参数never和另一个参数aways。设置成never
AS与JS之间就拒绝通信。设置成aways,不同域也可以访问。object里边还有一个属性是id。这个id与embed标签里边的name属性对应。在embed标签里边也有allowScriptAccess属性。其实,object标签和embed标签里边的属性很多都相同。想让swf背景透明就
将embed里边的wmode属性设置成”transparent”。
下边说下js代码。在js里边获取到swf的引用有两个方法。一个是用:
function getSwf(movieName) { if (navigator.appName.indexOf("Microsoft") != -1) { return window[movieName]; } else { return document[movieName]; } } getSwf("myFlash")
里边的参数就是embed标签里边的name和object标签里边的id。
另外一种方法是:
window.document.myFlash
myFlash,也是embed标签里边的name和object标签里边的id。不知道还有其他的方法没。对js不了解。获取到对swf的引用,就可以去调用as里边的方法了。直接在后边接 “.” ,再接AS那边的方法名,再接里边的参数。也可以不需要参数。看个人需要。
这里还有ifrme的调用。对iframe也很陌生。知道,通过调用,可以在网页调用的swf上覆盖一层页面。这里,可以做flash有时候不方便做,而页端方便做的事情。却不影响美观。具体搞法,看代码。
测试地址: http://vini123.sinaapp.com/demo/
下边贴出完整的代码:
AS_code:
package { import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.MouseEvent; import flash.external.ExternalInterface; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFieldType; import flash.text.TextFormat; public class AsToJs extends Sprite { private var sendBtn:Button; private var _txt:TextField; private var _txtFormat:TextFormat; private var _inputTxt:TextField; private var _step:int = 0 ; public function AsToJs() { stage.scaleMode = StageScaleMode.NO_SCALE; stage.align = StageAlign.TOP_LEFT; if(ExternalInterface.available) { ExternalInterface.addCallback("receiveMes",asReceiveMes); ExternalInterface.addCallback("backMes",jsBackMes); } Init(); } private function callJs(str:String):void { var len:int = str.length; var tempArr:Array; var _url:String; var _x:Number; var _y:Number; var _w:Number; var _h:Number; tempArr = str.split("-"); switch(int(tempArr[0])) { case 1: if(_step ==0 || _step == 3) { _url = tempArr[1]; ExternalInterface.call("loadIFrame",_url); if(_step !=3) { _step = 1; } printf("加载 Iframe"); } break; case 2: if(_step == 1) { ExternalInterface.call("showIFrame"); _step = 2; printf("显示 Iframe"); } break; case 3: if(_step >= 2) { _x = int(tempArr[1]); _y = int(tempArr[2]); _w = int(tempArr[3]); _h = int(tempArr[4]); ExternalInterface.call("moveIFrame",_x,_y,_w,_h); _step = 3; printf("移动 Iframe"); } break; } } private function asReceiveMes(str:String):void { printf("<font color='#ff0000'>JS发送,AS接受: </font>" + str); } private function jsBackMes(str:String):void { printf("<font color='#ff0000'>AS发送,JS返回: </font>" + str); } private function Init():void { InitBtn(); InitTxt(); printf("<font color='#ff0000'>Hi,Baby.This is a Demo. </font>"); } private function InitBtn():void { sendBtn = new Button("发送信息"); addChild(sendBtn); sendBtn.x = stage.stageWidth - sendBtn.width -10; sendBtn.y = stage.stageHeight - 36; sendBtn.buttonMode = true; sendBtn.addEventListener(MouseEvent.CLICK,clickHandler); } private function clickHandler(e:MouseEvent):void { if(ExternalInterface.available) { var numCode:int = int(_inputTxt.text.charAt(0)); if(numCode > 0) { callJs(_inputTxt.text); } else { ExternalInterface.call("sendMes",_inputTxt.text); } _inputTxt.text = ""; } } Boolean private function printf(...args):void { var len:int = args.length; for(var i:int =0 ; i< len ; i++) { if(_txt.numLines > 20) { _txt.text = ""; } _txt.htmlText += (args[i] + "\n"); } } private function InitTxt():void { _txtFormat = new TextFormat(); _txtFormat.color = 0x00ff00; _txtFormat.size = 13; _txt = new TextField(); _txt.autoSize = TextFieldAutoSize.LEFT; _txt.defaultTextFormat = _txtFormat; _txt.multiline = true; _txt.wordWrap = true; _txt.border = true; _txt.borderColor = 0x00ffff; _txt.width = stage.stageWidth - 20; _txt.height = stage.stageHeight - 50; addChild(_txt); _txt.x = 10; _txt.y = 10; _inputTxt = new TextField(); _inputTxt.autoSize = TextFieldAutoSize.LEFT; _txtFormat.color = 0xff0000; _inputTxt.defaultTextFormat = _txtFormat; _inputTxt.type = TextFieldType.INPUT; _inputTxt.wordWrap = true; _inputTxt.border = true; _inputTxt.borderColor = 0x00ffff; _inputTxt.width = stage.stageWidth - 100; addChild(_inputTxt); _inputTxt.x = 10; _inputTxt.y = sendBtn.y; } } } import flash.display.Sprite; import flash.events.MouseEvent; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFormat; class Button extends Sprite { private var _txt:TextField; private var _txtFormat:TextFormat; private var _deep:Sprite; private var _spaceW:int = 6; private var _spaceH:int = 3; public function Button(label:String):void { _txtFormat = new TextFormat(); _txtFormat.color = 0x00ff00; _txtFormat.size = 13; _txt = new TextField(); _txt.autoSize = TextFieldAutoSize.LEFT; _txt.text = label; _txt.setTextFormat(_txtFormat); _txt.mouseEnabled = false; addChild(_txt); draw(); this.addEventListener(MouseEvent.ROLL_OVER,overHandler); this.addEventListener(MouseEvent.ROLL_OUT,outHandler); } private function draw():void { _deep = new Sprite(); _deep.graphics.clear(); _deep.graphics.lineStyle(1.5,0x00ffff,0.8); _deep.graphics.beginFill(0xff00ff,1); _deep.graphics.drawRoundRect(0,0,_txt.textWidth + _spaceW *2 ,_txt.textHeight + _spaceH *2,5,5); _deep.graphics.endFill(); addChildAt(_deep,0); _txt.x = _spaceW -1.5; _txt.y = _spaceH - 1.5; } private function overHandler(e:MouseEvent):void { _txt.alpha = 0.7; } private function outHandler(e:MouseEvent):void { _txt.alpha = 1; } }
Html_code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <title> TEST AS TO JS </title> <head> <script type="text/javascript"> function getSwf(movieName) { if (navigator.appName.indexOf("Microsoft") != -1) { return window[movieName]; } else { return document[movieName]; } } function addMes(str) { var mes = (str.value == null)?str:str.value; getSwf("myFlash").receiveMes(mes); } function sendMes(str) { var mes = (str.value == null)?str:str.value; window.document.myFlash.backMes(mes); } //iframe // Major version of Flash required var requiredMajorVersion = 9; // Minor version of Flash required var requiredMinorVersion = 0; // Minor version of Flash required var requiredRevision = 0; function moveIFrame(x,y,w,h) { var frameRef=document.getElementById("myFrame"); frameRef.style.left=x; frameRef.style.top=y; var iFrameRef=document.getElementById("myIFrame"); iFrameRef.width=w; iFrameRef.height=h; } function hideIFrame() { document.getElementById("myFrame").style.visibility="hidden"; } function showIFrame() { document.getElementById("myFrame").style.visibility="visible"; } function loadIFrame(url) { document.getElementById("myFrame").innerHTML = "<iframe id='myIFrame' src='" + url + "'frameborder='0'></iframe>"; } </script> <body> <center> <object width = "550" height = "400" id = "myFlash"> <param name= "allowScriptAccess" value = "always"/> <param name= "movie" value = "AsToJs.swf" /> <param name = "quality" value = "high"/> <embed src = "AsToJs.swf" width = "550" height = "400" wmode = "transparent" name = "myFlash" allowScriptAccess = "always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"/> </center> </object> <form name = "form1" action = "#"> <input type = "text" name = "sendText" size = "65" maxlength = "200" /> <label> <input type = "button" name = "button" id = "button" value = "send to Flash" onclick = "addMes(sendText)" /> </label> </form> <!-- <iframe name="_history" src="history.htm" frameborder="0" scrolling="no" width="22" height="0"></iframe> --> <div id="myFrame" class = "box" style="top:0px;z-index:6;position:absolute;background-color:transparent;border:0px;visibility:hidden;"></div> <body> </html>