Adobe FLEX 2-PROGRAMMING ACTIONSCRIPT 3.0 Manual page 508

Programming actionscript 3.0
Table of Contents

Advertisement

Preparing for ActionScript-browser communication
One of the most common uses for the External API is to allow ActionScript applications to
communicate with a web browser. Using the External API, ActionScript methods can call
code written in JavaScript and vice versa. Because of the complexity of browsers and how they
render pages internally, there is no way to guarantee that a SWF document will register its
callbacks before the first JavaScript on the HTML page runs. For that reason, before calling
functions in the SWF document from JavaScript, your SWF document should always call the
HTML page to notify it that the SWF document is ready to accept connections.
For example, through a series of steps performed by the IMManager class, the Introvert IM
determines whether the browser is ready for communication and prepares the SWF file for
communication. The first step, determining when the browser is ready for communication,
happens in the IMManager constructor, as follows:
public function IMManager(initialStatus:IMStatus)
{
_status = initialStatus;
// Check if the container is able to use the External API.
if (ExternalInterface.available)
{
try
{
// This calls the isContainerReady() method, which in turn calls
// the container to see if Flash Player has loaded and the container
// is ready to receive calls from the SWF.
var containerReady:Boolean = isContainerReady();
if (containerReady)
{
// If the container is ready, register the SWF's functions.
setupCallbacks();
}
else
{
// If the container is not ready, set up a Timer to call the
// container at 100ms intervals. Once the container responds that
// it's ready, the timer will be stopped.
var readyTimer:Timer = new Timer(100);
readyTimer.addEventListener(TimerEvent.TIMER, timerHandler);
readyTimer.start();
}
}
...
}
else
{
trace("External interface is not available for this container.");
508
Using the External API

Advertisement

Table of Contents
loading

This manual is also suitable for:

Flex

Table of Contents