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.
3 Responses to Using the ExternalInterface class in ActionScript 2.0 and ActionScript 3.0
Leave a Reply Cancel reply
Recent Posts
- Getting started with the TLFTextField class in ActionScript 3.0 and Flash CS5
- Adding tick marks to a Slider control in Flash using ActionScript 3.0
- Creating a vertical Slider control in Flash using ActionScript 3.0
- Creating a custom context menu item in Flash using ActionScript 3.0
- Rotating a Sprite object around its x-axis in Flash using ActionScript 3.0 and Flash Player 10
- Detecting when the user changes the color in a ColorPicker control in Flash using ActionScript 3.0
- Getting the currently selected color as a hexadecimal value on a ColorPicker control in Flash using ActionScript 3.0
- Toggling the text field on the ColorPicker control in Flash using ActionScript 3.0
- Creating a vertical Slider control in Flash using ActionScript 3.0
- Setting the number of columns on a ColorPicker control in Flash using ActionScript 3.0
Categories
- Bitmap (1)
- Components (72)
- Button (19)
- CheckBox (2)
- ColorPicker (6)
- ComboBox (1)
- DataGrid (8)
- FLVPlayback (7)
- Label (9)
- ProgressBar (2)
- Slider (3)
- TextArea (1)
- TextInput (7)
- UILoader (7)
- ContextMenu (1)
- Embed (4)
- ExternalInterface (2)
- Flex (7)
- Font (2)
- General (5)
- Graphics (2)
- JSFL (14)
- Loader (3)
- LoadVars (3)
- Microphone (1)
- migration (12)
- MovieClip (1)
- MovieClipLoader (1)
- Sound (2)
- TextField (1)
- TLFTextField (1)
- TransitionManager (1)
- Tween (1)
- Uncategorized (1)
- URLLoader (4)
- URLVariables (1)
- Video (1)
- XML (2)
Advertising


Where is the Javascript interface code?
Now, there is a lot you can do with the ExternalInterface.
With my DOMExEventDispatcher Class you can let your swf listen for events from the wrapper. What that means is, for example with games, you can have the swf listen for keyboard events from the wrapper, and therefore no focus on the flash would be needed in order to register keyboard clicks! The same can be done for mouse events… it’s all here:
http://www.actiontad.com/components/src/domexeventExample/
Thanks for the detailed information on how to use the ExternalInterface with Action Script. The colored HTML code examples are particularly helpful for being able to identify the important elements within the coding. The time and effort taken to put this together are appreciated.