User Guide
Example: Using the External API with a web page container 511
The setCallBacks() method finishes the task of preparing for communication with the
container by calling
ExternalInterface.addCallback() to register the two methods that
will be available to be called from JavaScript. In this code, the first parameter—the name by
which the method is known to JavaScript (
"newMessage" and "getStatus")—is the same as
the method’s name in ActionScript. (In this case, there was no benefit to using different
names, so the same name was reused for simplicity.) Finally, the
ExternalInterface.call()
method is used to call the JavaScript function
setSWFIsReady(), which notifies the container
that the ActionScript functions have been registered.
Communication from ActionScript to the browser
The Introvert IM application demonstrates a range of examples of calling JavaScript functions
in the container page. In the simplest case (an example from the
setupCallbacks()
method), the JavaScript function
setSWFIsReady() is called without passing any parameters
or receiving a value in return:
ExternalInterface.call("setSWFIsReady");
In another example from the isContainerReady() method, ActionScript calls the
isReady() function and receives a Boolean value in response:
var result:Boolean = ExternalInterface.call("isReady");
You can also pass parameters to JavaScript functions using the External API. For instance,
consider the IMManager class’s
sendMessage() method, which is called when the user is
sending a new message to his or her “conversation partner:”
public function sendMessage(message:String):void
{
ExternalInterface.call("newMessage", message);
}
Once again, ExternalInterface.call() is used to call the designated JavaScript function,
notifying the browser of the new message. In addition, the message itself is passed as an
additional parameter to
ExternalInterface.call(), and consequently it is passed as a
parameter to the JavaScript function
newMessage().