当前位置:
首页 >
ActionScript
ActionScript与js交互代码
分类:ActionScript
来源:网络
时间:2011-07-23 00:22:30
- package {
- import flash.display.Sprite;
- import flash.events.*;
- import flash.external.ExternalInterface;
- import flash.text.TextField;
- import flash.utils.Timer;
- import flash.text.TextFieldType;
- import flash.text.TextFieldAutoSize;
- public class ExternalInterfaceExample extends Sprite {
- private var input:TextField;
- private var output:TextField;
- private var sendBtn:Sprite;
- public function ExternalInterfaceExample() {
- input = new TextField();
- input.type = TextFieldType.INPUT;
- input.background = true;
- input.border = true;
- input.width = 350;
- input.height = 18;
- addChild(input);
- sendBtn = new Sprite();
- sendBtn.mouseEnabled = true;
- sendBtn.x = input.width + 10;
- sendBtn.graphics.beginFill(0xCCCCCC);
- sendBtn.graphics.drawRoundRect(0, 0, 80, 18, 10, 10);
- sendBtn.graphics.endFill();
- sendBtn.addEventListener(MouseEvent.CLICK, clickHandler);
- addChild(sendBtn);
- output = new TextField();
- output.y = 25;
- output.width = 450;
- output.height = 325;
- output.multiline = true;
- output.wordWrap = true;
- output.border = true;
- output.text = "Initializing... ";
- addChild(output);
- if (ExternalInterface.available) {
- try {
- output.appendText("Adding callback... ");
- ExternalInterface.addCallback("sendToActionScript", receivedFromJavaScript);
- if (checkJavaScriptReady()) {
- output.appendText("JavaScript is ready. ");
- } else {
- output.appendText("JavaScript is not ready, creating timer. ");
- var readyTimer:Timer = new Timer(100, 0);
- readyTimer.addEventListener(TimerEvent.TIMER, timerHandler);
- readyTimer.start();
- }
- } catch (error:SecurityError) {
- output.appendText("A SecurityError occurred: " + error.message + " ");
- } catch (error:Error) {
- output.appendText("An Error occurred: " + error.message + " ");
- }
- } else {
- output.appendText("External interface is not available for this container.");
- }
- }
- private function receivedFromJavaScript(value:String):void {
- output.appendText("JavaScript says: " + value + " ");
- }
- private function checkJavaScriptReady():Boolean {
- var isReady:Boolean = ExternalInterface.call("isReady");
- return isReady;
- }
- private function timerHandler(event:TimerEvent):void {
- output.appendText("Checking JavaScript status... ");
- var isReady:Boolean = checkJavaScriptReady();
- if (isReady) {
- output.appendText("JavaScript is ready. ");
- Timer(event.target).stop();
- }
- }
- private function clickHandler(event:MouseEvent):void {
- if (ExternalInterface.available) {
- ExternalInterface.call("sendToJavaScript", input.text);
- }
- }
- }
- }
- <html lang="en">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>ExternalInterfaceExample</title>
- <script language="JavaScript">
- var jsReady = false;
- function isReady() {
- return jsReady;
- }
- function pageInit() {
- jsReady = true;
- document.forms["form1"].output.value += " " + "JavaScript is ready. ";
- }
- function thisMovie(movieName) {
- if (navigator.appName.indexOf("Microsoft") != -1) {
- return window[movieName];
- } else {
- return document[movieName];
- }
- }
- function sendToActionScript(value) {
- thisMovie("ExternalInterfaceExample").sendToActionScript(value);
- }
- function sendToJavaScript(value) {
- document.forms["form1"].output.value += "ActionScript says: " + value + " ";
- }
- </script>
- </head>
- <body onload="pageInit();">
- <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
- id="ExternalInterfaceExample" width="500" height="375"
- codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">
- <param name="movie" value="ExternalInterfaceExample.swf" />
- <param name="quality" value="high" />
- <param name="bgcolor" value="#869ca7" />
- <param name="allowScriptAccess" value="sameDomain" />
- <embed src="ExternalInterfaceExample.swf" quality="high" bgcolor="#869ca7"
- width="500" height="375" name="ExternalInterfaceExample" align="middle"
- play="true" loop="false" quality="high" allowScriptAccess="sameDomain"
- type="application/x-shockwave-flash"
- pluginspage="http://www.macromedia.com/go/getflashplayer">
- </embed>
- </object>
- <form name="form1" onsubmit="return false;">
- <input type="text" name="input" value="" />
- <input type="button" value="Send" onclick="sendToActionScript(this.form.input.value);" /><br />
- <textarea cols="60" rows="20" name="output" readonly="true">Initializing...</textarea>
- </form>
- </body>
- </html>
- 默认分类(20)
- J2EE(25)
- Java(56)
- PHP(55)
- SEO(10)
- 网页设计(20)
- 网站建设(37)
- 数据库(7)
- JavaScript(17)
- JQuery(6)
- MySQL(20)
- SQL Server(6)
- Access(1)
- Oracle(6)
- office(6)
- Dreamweaver(4)
- Photoshop(12)
- Flash(9)
- Fireworks(13)
- CSS(14)
- HTML(4)
- .NET(7)
- ASP(2)
- DB2(1)
- Ajax(2)
- Linux(12)
- Struts(7)
- Hibernate(8)
- Spring(2)
- Jsp(22)
- Asp(8)
- C#(3)
- C++(1)
- 网络安全(5)
- 软件工程(7)
- XML(1)
- English(2)
- 计算机等级考试(2)
- 计算机病毒(4)
- 个人日志(76)
- 互联网(15)
- ActionScript(10)
- Android(3)
- 数据结构与算法(1)
- 游戏策略(3)
- 美文翻译(2)
- 编程开发(19)
- 计算机应用(4)
- 计算机(10)
- Unity3d(6)
- 其他(1)
- egret(1)