Entry: js flash communication

In the above example you can draw circles or squares on the upper screens and the used data like x and y coordinate, the shape and the color are sent to JavaScript, partly shown under these to screens and sent to the bottom screen.
If you youse your mousewheel you can actually change the radius of you brush but that information is not sent to Javascript and back it is rather sent through a local connection directly from flash to flash.

There are many reasons you might want to have a working communication between a swf file and JavaScript. You could use JavaScript browser checks and send the retreived data to a swf file that reacts in different ways, or you could integrate swf files in your AJAX framework easily.

In Actionscript it is extremly easy to establish such a connection with the ExternalInterface. If you want to sent data from Flash to JavaScript you can do it like this:
function saySomething(text){
ExternalInterface.call("sawSomething", "Hello JavaScript");

The other way around is just as easy. To sent data from Javascript to Flash all you got to do is to create a callback function in Actionscript and call that function from JavaScript:
window.document.swfFileName.answer("Hello Actionscript");
ExternalInterface.addCallback("answer", answerJSCall);
function answerJSCall(string:String):void{
Output.text = "JavaScript said " + string;
"swfFileName" is the id or name of your embedded swf file in which the callback function is defined. To make the example work you would need a textfield called "Output" on your stage.

To send information directly between swf files you can use a local connection:
Both files need to open one and the same connection to communicate with each other.
var connection:LocalConnection = new LocalConnection();
connection.addEventListener(StatusEvent.STATUS, statusCheck);

function statusCheck(event:StatusEvent):void {
switch (event.level){
case "status":
Status.text = "LocalConnection.send() succeeded";
case "error":
Status.text = "LocalConnection.send() failed";

function scrollEvent(event:MouseEvent):void{
pointRadius += event.delta;
Size.text = Math.abs(pointRadius);
connection.client = this;
connection.send("connection", "changeRadius", pointRadius);
// local connection
var connection = new LocalConnection();
connection.client = this;
Status.text = "can't connect because the connection name is already being used by another SWF";

// to be called from local connection
function changeRadius(radius:Number){
pointRadius = radius;
Size.text = Math.abs(pointRadius);
To provoke an error you could open this very page in two tabs so in one the local connection to the screenpad will not be established cause it is already in the other one.
That's it. As you see it's pretty easy to communicate with the JavaScript world outside of flash but it gets a little more challenging when you have to deal with flash's new security model. If the communicating files are distributed over several different domains the security policy files or crossdomain.xml files become interesting.
download source


Creative Commons License