[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 >
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>
时间: 2024-10-05 14:03:48

[ActionScript 3.0] 与js通信的相关文章

[ActionScript 3.0] 与C#通信方法fscommand

与C#通信的方法之一 flash.system.fscommand(command:String, args:String=""):void, command 传递给主机应用程序的用于任何用途的一个字符串,或传递给 Flash Player 的一个命令. args 传递给主机应用程序的用于任何用途的一个字符串,或传递给 Flash Player 的一个值. 若同时发送两个或两个以上请求,需注意一个问题,例如 fscommand("cmdName1"); fscomm

as3.0和js相互调用,js控制flash的颜色

程序应用场景:flash是一副地图,地图上一单击,就调用js,弹出一个对话框,显示一些自己想要的信息:js定时调用as提供的方法,然后去改变flash的颜色. 以下是as代码: import flash.events.MouseEvent; import flash.text.TextField; //给每一栋楼增加监听事件 mc0001.addEventListener(MouseEvent.CLICK,onClick); mc0004.addEventListener(MouseEvent.

flex与js通信、在浏览器中打开新窗口

一.flex与js通信(通过flex调用js方法) var urlR:URLRequest = new URLRequest("javascript:test('from flex')"); navigateToURL(urlR, "_self"); 其中URLRequest中的js方法必须用引号test是js方法名,'from flex'是该js方法的参数. 另一种相互通信方法: ExternalInterface.addCallback("getKyq

ActionScript 3.0 for the Lunder Algorithm

package com.feiruo.Calendar.LunderCalendar { /* *@ClassName: package:com.feiruo.Calendar.LunderCalendar::LunarDate *@INTRO: Calculating lunar and solar terms and other settings. *@Author: feiruo *@Language: ActionScript 3.0 *@SDKVersion: Flash Player

在 Flash ActionScript 2.0 中调用 Javascript 方法

本篇文章由:http://xinpure.com/call-the-javascript-method-in-flash-actionscript-2-0/ 在 Flash ActionScript 2.0 中调用 Javascript 方法 最近在工作中,有个这样的需求: 要从 Flash ActionScript 2.0 中调用网页上的 Javascript 方法 这是一个关于 Flash 和 Javascript 交互的问题. 在 ActionScript 2.0 中调用外部 javasc

ActionScript 3.0日期与时间管理(Date类)

/*Flash中,Date类管理着日期和时间信息.要获取   当前日期,可以新建一个Date对象   */var now:Date =new Date();trace (now);     /*输出当前时间和日期,格式如:                  Sun Jul 27 19:45:57 GMT+0800 2014  */ /*以上代码中,Date对象中包括了当前的秒.分.小时.  星期数.日.月.年.时区等信息...如果仅需要返  回星期值和日期值,而不返回时间或时区,可以使用Date

window之间、iframe之间的JS通信

一.Window之间JS通信 在开发项目过程中,由于要引入第三方在线编辑器,所以需要另外一个窗口(window),而且要求打开的window要与原来的窗口进行js通信,那么如何实现呢? 1.在原窗口创建新打开window的一个对象: var new_window; var url = 'http:://second.com'; new_window = window.open(url, 'new_window'); 新窗口里有一个say()方法: function say() { alert('

MyEclipse导入jquery-1.8.0.min.js等文件报错的解决方案

MyEclipse导入jquery-1.8.0.min.js等文件报错的解决方案 版权声明:本文为博主原创文章,未经博主允许不得转载. MyEclipse导入jQuery-1.8.0.min.js等文件的时候有时候会报了一堆missing semicolon的错误.怎么解决这个报错呢?方法如下: 1.选中报错的jquery文件例如“jquery-1.8.0.min.js”. 2.右键选择 MyEclipse-->Exclude From Validation . 3.再右键选择 MyEclips

VC++ 6.0 C8051F340 USB 通信 CAN 数据解析

// HelloWorld.cpp : Defines the entry point for the console application. // /***************************************************************************** * VC++ 6.0 C8051F340 USB 通信 CAN 数据解析 * 声明: * 1. 这是在<VC++ 6.0 C8051F340 USB PC侧通信 Demo>的基础上的代码: