[ActionScript 3.0] 与js通信
AS3代码,可做文档类;
1 package { 2 import flash.display.Sprite; 3 import flash.events.*; 4 import flash.external.ExternalInterface; 5 import flash.text.TextField; 6 import flash.utils.Timer; 7 import flash.text.TextFieldType; 8 import flash.text.TextFieldAutoSize; 9 10 public class ExternalInterfaceExample extends Sprite { 11 private var input:TextField; 12 private var output:TextField; 13 private var sendBtn:Sprite; 14 15 public function ExternalInterfaceExample() { 16 input = new TextField(); 17 input.type = TextFieldType.INPUT; 18 input.background = true; 19 input.border = true; 20 input.width = 350; 21 input.height = 18; 22 addChild(input); 23 24 sendBtn = new Sprite(); 25 sendBtn.mouseEnabled = true; 26 sendBtn.x = input.width + 10; 27 sendBtn.graphics.beginFill(0xCCCCCC); 28 sendBtn.graphics.drawRoundRect(0, 0, 80, 18, 10, 10); 29 sendBtn.graphics.endFill(); 30 sendBtn.addEventListener(MouseEvent.CLICK, clickHandler); 31 addChild(sendBtn); 32 var t:TextField = new TextField(); 33 t.text = "Send"; 34 t.selectable = false; 35 t.autoSize = TextFieldAutoSize.LEFT; 36 t.x = (sendBtn.width - t.width) / 2; 37 t.y = (sendBtn.height - t.height) / 2; 38 sendBtn.addChild(t); 39 40 output = new TextField(); 41 output.y = 25; 42 output.width = 450; 43 output.height = 325; 44 output.multiline = true; 45 output.wordWrap = true; 46 output.border = true; 47 output.text = "Initializing...\n"; 48 addChild(output); 49 50 if (ExternalInterface.available) { 51 try { 52 output.appendText("Adding callback...\n"); 53 ExternalInterface.addCallback("sendToActionScript", receivedFromJavaScript);//as接收js发送过来的信息 54 if (checkJavaScriptReady()) { 55 output.appendText("JavaScript is ready.\n"); 56 } else { 57 output.appendText("JavaScript is not ready, creating timer.\n"); 58 var readyTimer:Timer = new Timer(100, 0); 59 readyTimer.addEventListener(TimerEvent.TIMER, timerHandler); 60 readyTimer.start(); 61 } 62 } catch (error:SecurityError) { 63 output.appendText("A SecurityError occurred: " + error.message + "\n"); 64 } catch (error:Error) { 65 output.appendText("An Error occurred: " + error.message + "\n"); 66 } 67 } else { 68 output.appendText("External interface is not available for this container."); 69 } 70 } 71 private function receivedFromJavaScript(value:String):void { 72 output.appendText("JavaScript says: " + value + "\n"); 73 } 74 private function checkJavaScriptReady():Boolean { 75 var isReady:Boolean = ExternalInterface.call("isReady"); 76 return isReady; 77 } 78 private function timerHandler(event:TimerEvent):void { 79 output.appendText("Checking JavaScript status...\n"); 80 var isReady:Boolean = checkJavaScriptReady(); 81 if (isReady) { 82 output.appendText("JavaScript is ready.\n"); 83 Timer(event.target).stop(); 84 } 85 } 86 private function clickHandler(event:MouseEvent):void { 87 if (ExternalInterface.available) { 88 ExternalInterface.call("sendToJavaScript", input.text);//as向js发送信息 89 } 90 } 91 } 92 }
JS代码,方式一;
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html> 3 <head> 4 <title>ExternalInterfaceExample</title> 5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 6 <meta name="language" content="en" /> 7 <meta name="description" content="" /> 8 <meta name="keywords" content="" /> 9 <script language="JavaScript"> 10 var jsReady = false; 11 function isReady() { 12 return jsReady; 13 } 14 function pageInit() { 15 jsReady = true; 16 document.forms["form1"].output.value += "\n" + "JavaScript is ready.\n"; 17 } 18 function thisMovie(movieName) { 19 if (navigator.appName.indexOf("Microsoft") != -1) { 20 return window[movieName]; 21 } else { 22 return document[movieName]; 23 } 24 } 25 function sendToActionScript(value) { 26 thisMovie("ExternalInterfaceExample").sendToActionScript(value); 27 } 28 function sendToJavaScript(value) { 29 document.forms["form1"].output.value += "ActionScript says: " + value + "\n"; 30 } 31 </script> 32 33 </head> 34 <body onload="pageInit();"> 35 36 <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" 37 id="ExternalInterfaceExample" width="500" height="375" 38 codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab"> 39 <param name="movie" value="ExternalInterfaceExample.swf" /> 40 <param name="quality" value="high" /> 41 <param name="bgcolor" value="#869ca7" /> 42 <param name="allowScriptAccess" value="sameDomain" /> 43 <embed src="ExternalInterfaceExample.swf" quality="high" bgcolor="#869ca7" 44 width="500" height="375" name="ExternalInterfaceExample" align="middle" 45 play="true" loop="false" quality="high" allowScriptAccess="sameDomain" 46 type="application/x-shockwave-flash" 47 pluginspage="http://www.macromedia.com/go/getflashplayer"> 48 </embed> 49 </object> 50 51 <form name="form1" onsubmit="return false;"> 52 <input type="text" name="input" value="" /> 53 <input type="button" value="Send" onclick="sendToActionScript(this.form.input.value);" /><br /> 54 <textarea cols="60" rows="20" name="output" readonly="true">Initializing...</textarea> 55 </form> 56 57 </body> 58 59 </html>
JS代码,方式二;(需要文件 swfobject.js)
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html> 3 <head> 4 <title>ExternalInterfaceExample</title> 5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 6 <meta name="language" content="en" /> 7 <meta name="description" content="" /> 8 <meta name="keywords" content="" /> 9 <script type="text/javascript" src="swfobject.js"></script> 10 <script type="text/javascript"> 11 swfobject.registerObject("ExternalInterfaceExample", "9.0.0", "expressInstall.swf", callbackFn); 12 13 function sendToJavaScript(value) { 14 document.getElementById("output").value += "ActionScript says: " + value + "\n"; 15 } 16 17 function ExternalInterfaceExample_DoFSCommand(command, args) {//此方法是用于fscommand() 与js通信,在此可忽略 18 document.getElementById("output").value += "ActionScript says: " + args + "\n"; 19 } 20 21 function callbackFn(status) { 22 if (status.success) { 23 var obj = status.ref; 24 document.getElementById("but").onclick = function() { 25 if (obj && typeof obj.sendToActionScript!= "undefined") { 26 obj.sendToActionScript(document.getElementById("input").value); 27 } 28 }; 29 } 30 } 31 32 </script> 33 34 </head> 35 <body> 36 37 <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" 38 id="ExternalInterfaceExample" width="500" height="375" 39 codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab"> 40 <param name="movie" value="ExternalInterfaceExample.swf" /> 41 <param name="quality" value="high" /> 42 <param name="bgcolor" value="#869ca7" /> 43 <param name="allowScriptAccess" value="sameDomain" /> 44 <embed src="ExternalInterfaceExample.swf" quality="high" bgcolor="#869ca7" 45 width="500" height="375" name="ExternalInterfaceExample" align="middle" 46 play="true" loop="false" quality="high" allowScriptAccess="sameDomain" 47 type="application/x-shockwave-flash" 48 pluginspage="http://www.macromedia.com/go/getflashplayer"> 49 </embed> 50 </object> 51 52 <form name="form1" onsubmit="return false;"> 53 <input type="text" name="input" id="input" value="" /> 54 <input type="button" value="ExternalInterface" id="but"/><br /> 55 <textarea cols="60" rows="20" name="output" id="output" readonly="true">Initializing...</textarea> 56 </form> 57 </body> 58 59 </html>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。