User Guide
Example: Using the External API with a web page container 509
}
}
First of all, the code checks whether the External API is even available in the current container
using the
ExternalInterface.available property. If so, it begins the process of setting up
communication. Because security exceptions and other errors can occur when you attempt
communication with an external application, the code is wrapped in a
try block (the
corresponding
catch blocks were omitted from the listing for brevity).
The code next calls the
isContainerReady() method, listed here:
private function isContainerReady():Boolean
{
var result:Boolean = ExternalInterface.call("isReady");
return result;
}
The isContainerReady() method in turn uses ExternalInterface.call() method to call
the JavaScript function
isReady(), as follows:
<script language="JavaScript">
<!--
// ------- Private vars -------
var jsReady = false;
...
// ------- functions called by ActionScript -------
// called to check if the page has initialized and JavaScript is available
function isReady()
{
return jsReady;
}
...
// called by the onload event of the <body> tag
function pageInit()
{
// Record that JavaScript is ready to go.
jsReady = true;
}
...
//-->
</script>
The isReady() function simply returns the value of the jsReady variable. That variable is
initially
false; once the onload event of the web page has been triggered, the variable’s value
is changed to
true. In other words, if ActionScript calls the isReady() function before the
page is loaded, JavaScript returns
false to ExternalInterface.call("isReady"), and
consequently the ActionScript
isContainerReady() method returns false. Once the page
has loaded, the JavaScript
isReady() function returns true, so the ActionScript
isContainerReady() method also returns true.