Using the ExternalInterface class in ActionScript 2.0 and ActionScript 3.0

by Peter deHaan on February 28, 2008

in ExternalInterface, migration

The following example(s) show how you can use the ExternalInterface class (flash.external.ExternalInterface) in ActionScript 2.0 and ActionScript 3.0 to send a string from Flash to the HTML container where it is displayed using the JavaScript alert() function.

Full code after the jump.

The first example shows how you can check whether the ExternalInterface API is available using the static ExternalInterface.available property (note, I’m not checking whether or not it is available or not, I’m just displaying the value). First we craft our XML-node as a string, and then pass it to the HTML container using the static ExternalInterface.call() method, which calls the alert() function, passing the XML string as a String object:

// ActionScript 2.0
/**
 * Requires:
 *   - A Label component on the Stage with an instance name of "lbl".
 *   - A Button component on the Stage with an instance name of "button".
 */
import flash.external.*;
 
var xmlResponse:String = "<invoke name=\"isReady\" returntype=\"xml\"><arguments><number>1</number><number>" + Stage.width + "</number><number>" + Stage.height + "</number></arguments></invoke>";
 
lbl.text = "ExternalInterface.available: " + ExternalInterface.available;
lbl._width = 200;
button.enabled = ExternalInterface.available;
button.addEventListener("click", button_click);
 
function button_click(evt:Object):Void {
    ExternalInterface.call("alert", xmlResponse);
}

And here is the same code in ActionScript 3.0:

// ActionScript 3.0
/**
 * Requires:
 *   - A Label component on the Stage with an instance name of "lbl".
 *   - A Button component on the Stage with an instance name of "button".
 */
var xmlResponse:String = "<invoke name=\"isReady\" returntype=\"xml\"><arguments><number>1</number><number>" + stage.stageWidth + "</number><number>" + stage.stageHeight + "</number></arguments></invoke>";
 
lbl.text = "ExternalInterface.available: " + ExternalInterface.available;
lbl.width = 200;
button.enabled = ExternalInterface.available;
button.addEventListener(MouseEvent.CLICK, button_click);
 
function button_click(evt:MouseEvent):void {
    ExternalInterface.call("alert", xmlResponse);
}

Overall, the code is fairly similar. You’ll notice that you need to import the flash.external.ExternalInterface class (or the flash.external.* package) in ActionScript 2.0, but not in ActionScript 3.0. Apart from that, the only real differences are ActionScript 2.0’s “_width” property on the Label control, instead of ActionScript 3.0’s “width” property. And also, the Button control’s event handler syntax is slightly different between the two flavors of ActionScript.

This was an oversimplified example. Normally you would want to call a custom JavaScript function or do something a bit more interesting.

{ 1 comment… read it below or add one }

1 David 09.16.09 at 4:30 pm

Where is the Javascript interface code?

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Previous post:

Next post: