User Guide

506 Using the External API
Calling ActionScript code from the container
A container can only call ActionScript code thats in a function—no other ActionScript code
can be called by a container. To call an ActionScript function from the container application,
you must do two things: register the function with the ExternalInterface class, and then call it
from the containers code.
First, you must register your ActionScript function to indicate that it should be made available
to the container. Use the
ExternalInterface.addCallback() method, as follows:
function callMe(name:String):String
{
return "busy signal =)";
}
ExternalInterface.addCallback("myFunction", callMe);
The addCallback() method takes two parameters. The first, a function name as a String, is
the name by which the function will be known to the container. The second parameter is the
actual ActionScript function that will be executed when the container calls the defined
function name. Because these names are distinct, you can specify a function name that will be
used by the container, even if the actual ActionScript function has a different name. This is
especially useful if the function name is not known—for example, if an anonymous function
is specified, or if the function to be called is determined at run time.
Once an ActionScript function has been registered with the ExternalInterface class, the
container can actually call the function. How this is done varies according to the type of
container. For example, in JavaScript code in a web browser, the ActionScript function is
called using the registered function name as though it’s a method of the Flash Player browser
object (that is, a method of the JavaScript object representing the
<object> or <embed> tag).
In other words, parameters are passed and a result is returned as though a local function is
being called.
<script language="JavaScript">
// callResult gets the value "busy signal =)"
var callResult = flashObject.myFunction("my name");
</script>
...
<object id="flashObject"...>
...
<embed name="flashObject".../>
</object>
Alternatively, when calling a function in a SWF file running in an embedded ActiveX control,
the registered function name and any parameters must be serialized into an XML-formatted
string. Then the call is actually performed by calling the
CallFunction() method of the
ActiveX control with the XML string as a parameter.