View comments | RSS feed

Calling ActionScript code from the container

A container can only call ActionScript code that's 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 container's 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 an ActionScript function in a SWF file running in a desktop application, 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. For more information about the XML format used for this communication, see The external API's XML format.

In either case, the return value of the ActionScript function is passed back to the container code, either directly as a value when the caller is JavaScript code in a browser, or serialized as an XML-formatted string when the caller is an ActiveX container.


Flash CS3


Comments


No screen name said on Jul 28, 2007 at 10:23 AM :
Have a error...
Return the "flashObject.myFunction" is not a function!

Tested In IE7 and FF2.

Someone obtained it?
Thanks.
mlmorr0 said on Nov 2, 2007 at 12:03 AM :
It's an error in the documentation. You need to get the object, not use it's string. Correct code is as follows:

<script language="JavaScript">
// callResult gets the value "busy signal"
var flashObj = document.getElementById('flashObject');
var callResult = flashObj.myFunction("my name");
</script>

If you use the prototype framework you can do the following.

<script language="JavaScript">
// callResult gets the value "busy signal"
var callResult = $('flashObject').myFunction("my name");
</script>

Another thing the documentation forgets to mention - the flash object must have been drawn into the DOM before you can invoke any of its functions. This is particular important if you are drawing the movie into the DOM to get around the IE7 "feature" of requiring click ons to activate swf objects. Also if you have a particularly beefy amount of script that the movie has to use to initialize itself you might want to set up a call to javascript to tell your javascript that the movie is ready to receive calls.

I'm working on some code that extends prototype/scriptaculous to smooth out crosstalk between javascript and actionscript. Once I'm sure of its stability I intend to release it under MIT license sometime early next year.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00000343.html