User Guide
512 Using the External API
Calling ActionScript code from JavaScript
Communication is supposed to be a two-way street, and the Introvert IM application is no
exception. Not only does the Flash Player IM client call JavaScript to send messages, but the
HTML form calls JavaScript code to send messages to and ask for information from the SWF
file as well. For example, when the SWF file notifies the container that it has finished
establishing contact and it’s ready to communicate, the first thing the browser does is call the
IMManager class’s
getStatus() method to retrieve the initial user availability status from the
SWF IM client. This is done in the web page, in the
updateStatus() function, as follows:
<script language="JavaScript">
...
function updateStatus()
{
if (swfReady)
{
var currentStatus = getSWF("IntrovertIMApp").getStatus();
document.forms["imForm"].status.value = currentStatus;
}
}
...
</script>
The code checks the value of the swfReady variable, which tracks whether the SWF file has
notified the browser that it has registered its methods with the ExternalInterface class. If the
SWF file is ready to receive communication, the next line (
var currentStatus = ...) actually
calls the
getStatus() method in the IMManager class. Three things happen in this line of
code:
■ The getSWF() JavaScript function is called, returning a reference to the JavaScript object
representing the SWF file. The parameter passed to
getSWF() determines which browser
object is returned in case there is more than one SWF file in an HTML page. The value
passed to that parameter must match the
id attribute of the <object> tag and name
attribute of the
<embed> tag used to embed the SWF file.
■ Using the reference to the SWF file, the getStatus() method is called as though it’s a
method of the SWF object. In this case the function name “
getStatus” is used because
that’s the name under which the ActionScript function is registered using
ExternalInterface.addCallback().