Example for the tutorial on actionscript javascript communication by circlecube.com

The form on the left is fully html/javascript/css and pressing the button will send the text over to the flash version which is in as3. The form on the right is full flash and receives text from the javascript form as well as sends text back. They are both set to clear once they send data so you can easily watch the text string jump from one to the other.


HTML/JavaScript


Javascript


//javascript
function receiveTextFromAS3(Txt) {
	document.getElementById('htmlText').value = Txt;
}
function sendTextToAS3(){
	var Txt = document.getElementById('htmlText').value;
	var flash =	document.getElementById("as3_js");
	flash.sendTextFromJS(Txt);
	document.getElementById('htmlText').value = "";
}

var flashvars = {};
var params = {};
var attributes = {};
attributes.id = "as3_js";
swfobject.embedSWF("AS3_Javascript.swf", "alt", "450", "450", "9.0.0", false, flashvars, params, attributes);

HTML


//html
<textarea
 name="htmlText"
 id="htmlText"
 cols="50"
 rows="15"> 
</textarea>

<input
 type="button"
 onclick="sendTextToAS3();" 
 name="sendToFlash"
 id="sendToFlash" 
 value="Send Text To Flash"
/>

<div id="alt"></div>

Flash/AS3


//actionscript - as3
import flash.external.ExternalInterface;

//Set up Javascript to Actioscript
ExternalInterface.addCallback("sendTextFromJS", receiveTextFromJS);
function receiveTextFromJS(t:String):void {
	theText.text = t;
}


//Actionscript to Javascript
function sendTextFromAS3(e:MouseEvent):void {
	ExternalInterface.call("receiveTextFromAS3", theText.text);
	theText.text = "";
}
button.addEventListener(MouseEvent.CLICK, sendTextFromAS3);
button.buttonMode = true;