- Home
- Tutorials
- Flash
- Intermediate
- Basics of using the ExternalInterface

ExternaInterface communication gateway
Matt Kenefick
PHP / Actionscript Developer
Matt Kenefick is primarily an Actionscript developer with experience developing sites, DVD's, print, and interactive work for companies like Nike, Kenneth Cole, Calvin Klein, Smirnoff - Diageo, Martha Stewart and more. Currently resides and works in New York, NY. Programs in other languages such as PHP, JS/AJAX, VB, and C. Also shoots photography and does intensive graphic work.
The ExternalInterface is a fantastic way to communicate with
Javascript directly from Flash. It is very similar to fscommand() , CallLabel(), and CallFrame(). The External Interface is much
more flexible. You can pass as many arguments as you want to any function on
the HTML page and receive a return value. It works in the opposite way as well.
You can call Actionscript functions from Javascript and receive a return value
immediately.
The ExternalInterface is an extension of the Object class. It requires the
user's browser to support either ActiveX or NPRuntime API. (
http://www.mozilla.org/projects/plugins/npruntime.html ) It works on the
following browsers: IE5+, Netscape 8.0+, Mozilla 1.7.5+, Firefox 1.0+, and
Safari 1.3+.
Available : Flash 8; AS 1.0
Before we get into examples and details about the class, we'll list out the
properties and methods for reference.
Properties
|
Modifiers |
Property |
Description |
|
static |
available:Boolean |
Returns whether or not the player is able of offering |
Properties Inherited from Object extension
|
Modifiers |
Property |
Description |
|
constructor:Object |
Reference to the constructor function for a given |
|
|
__proto__:Object |
Refers to the prototype property of the class or constructor function used to create the object. |
|
|
static |
prototype:Object |
A reference to the superclass of a class or function object. |
|
__resolve:Object |
A reference to a user-defined function that is invoked if Actionscript code refers to an undefined property or method. |
Methods
|
Modifiers |
Signature |
Description |
|
static |
addCallback( methodName:String, instance:Object, method:Function ):Boolean |
Registers an Actionscript method as callable from the container. |
|
static |
call( methodName:String, [parameter1:Object ] ) : Object |
Calls a function exposed by the Flash Player container, passing 0 or more arguments. |
Methods Inherited from Object extension
addProperty; hasOwnProperty; isPropertyEnumerable;
isPrototypeOf; registerClass; toString; unwatch**; valueOf; watch**
** These are very useful but commonly overlooked methods of the Object class. I
have an additional article dedicated to these methods.
This class is implemented into your file by calling:
import flash.external.ExternalInterface
The following is an example of communicating with Javascript from Actionscript.
Start off by creating a new AS 2.0 project in Flash, create a dynamic textbox on the Stage with the instance name of "return_txt". Fill it with some default text so you can see the change. This code will apply the JS function to the onMouseDown event, so clicking anywhere will show you how it works.
import flash.external.ExternalInterface
// import the external interface class
function onChange( str:String ){
return_txt.text = "Javascript Return: " + str;
// the return function
}
ExternalInterface.addCallback( "onChange", this, onChange );
// tells it the property in JS, the scope, and the return function to call
onMouseDown = function(){
ExternalInterface.call("general_JS_call", "String to return to Flash:");
// calling the function name first, arguments of the function following
}
HTML / JS Code
<script>
var me;
function getID( swfID ){
if(navigator.appName.indexOf("Microsoft") != -1){
me = window[swfID];
}else{
me = document[swfID];
}
}
function js_to_as( str ){
me.onChange(str);
}
function general_JS_call( str ){
me.onChange( prompt(str) );
}
</script>
<body onLoad='getID("jsExample");'>
<embed src='test.swf' height='400' width='550' id='jsExample' >
<input type="button" value="Pass to AS" onClick="js_to_as('Test value to send to AS');">
So This HTML code will call the getID function onLoad to determine
what kind of browser you're using for the DOM. The general_JS_call is
called from the Actionscript code. The .onChange applied to me is
also defined in the Actionscript.
Important Notice: When passing the variable back to Flash,
don't set the onChange = str. You must use parentheses asif you are calling a
function. me.onChange(str)
As you can see when you test this code, the argument passed from AS
"String to return to Flash:" will show up on the prompt called in general_JS_call.
This is a great example of how passing arguments to Javascript from Flash
works.
This also demonstrates how to pass variables from Javascript to Actionscript.
This is a brief and generic overview of
how to use the ExternalInterface class. It shows the ease and reliability of
using the ExternalInterface for Javascript calls to and from Flash. Look out
for more in-depth and deeper examples.
You can use these examples to get very elaborate and involved based on your JS
and AS knowledge.
Spread The Word
17 Responses to "Basics of using the ExternalInterface" 
|
said this on 11 Jul 2007 10:30:27 AM CDT
Thanks! Can you do this o
|
|
said this on 23 Jul 2007 11:59:22 AM CDT
This only worked in fireF
|
|
said this on 25 Jul 2007 9:30:18 AM CDT
I have been unable to mak
Clic When I c Any help would b |
|
said this on 25 Jul 2007 1:30:43 PM CDT
Earlier today, I submitte
After gettin |
|
said this on 13 Nov 2008 7:14:44 AM CDT
thanx for this post!
2 |
|
said this on 24 Feb 2008 8:18:33 AM CDT
Excellent tutorial for th
N http:/ |
|
said this on 10 Sep 2008 8:01:59 AM CDT
Getting a javascript erro
|
|
said this on 14 Apr 2010 11:57:39 AM CDT
Thanks, I was getting so
|
|
said this on 14 Nov 2008 12:55:58 AM CDT
I discovered the External
My External Interfac I love External Interfac |
|
said this on 25 Feb 2009 2:15:10 AM CDT
I'm using external AS (2)
Does anyone know .Jorn |
|
said this on 04 Aug 2009 5:45:51 PM CDT
Look at the Delegate clas
|
|
said this on 06 Aug 2009 11:10:40 AM CDT
I followed this excellent
After googlein 1) The callBack functio publ 2) Hope thi |
|
said this on 28 Oct 2009 1:47:36 PM CDT
Yes! Thank you. Been bang
I wrapped th Javascript if } else { } ... |
|
said this on 03 Feb 2010 6:44:21 AM CDT
Great, this performs the
|
|
said this on 15 Mar 2010 7:44:12 AM CDT
Wonderful and working fin
Thanks again for yo |
|
said this on 11 Mar 2011 4:30:27 AM CDT
I'd been getting an error
Hopefully t |


Author/Admin)