The ExternalInterface Class
The ExternalInterface class is designed to be able to communicate with external functionality such as JavaScript.It is important that when using this class to import it first at the beginning of your script, like this:
import flash.external.ExternalInterface;
Properties
available
Availability :
FP:8, AS:1.0Generic Template :
ExternalInterface.available;Description: The property indicates whether the player it is residing in is capable of offering an external interface; true if yes and false if no.Example: This example will send to the Output panel the availability status of an external interface:
import flash.external.ExternalInterface;
trace(ExternalInterface.available);
Methods
addCallback
Availability :
FP:8, AS:1.0Generic Template :
ExternalInterface.addCallback(nickName, instance, function);Returns :
BooleanThis will indicate if the call succeeded. If it did not, check the allScriptAccess attribute in both the OBJECT and EMBED tags to make sure you are not in a security violation.Parameters:
nickName
This string is what the external interface will call the method with. It does not have to be the same name as the function in the timeline.instance
This object represents what "this" will represent in the function being called.function
This is the actual function being called on the timeline.
Description: This method will add the capability for an external interface to be able to call a function on the Flash timeline.Example: This example will send to the Output panel the availability status of an external interface:
And then in th296, you might have something like this:
import flash.external.ExternalInterface;
//the function
function visitSams() {
getURL("http://www.sams.com", "_blank");
}
//add the callback
ExternalInterface.addCallback("visitSams", this, visitSams);
NOTEWhen setting up the JavaScript to call the Flash function, you must make sure the id attribute of the object tag and the name attribute of the embed tag are set to the same thing.
<script>
function goOut() {
document.getElementById("visitSams").flash.visitSams();
}
</script>
<form>
<input type="button" onclick="goOut()" value="Go to Sams" />
</form>
call
Availability :
FP:8, AS:1.0Generic Template :
ExternalInterface.call(function, parameters);Returns :
ObjectThis will be the response from the external function being called if there is one. If there is no return value, or the method failed, null will be returned.Parameters:
function
This string is the name of the function in the external interface.parameters
These are the parameters that are passed to the external function, each separated by a comma. This is an optional parameter.
Description: This method will allow Flash to call functions in the external interface as well as pass parameters to them.Example: This example will call the alert function in JavaScript, passing it a simple string:
Again, if this does not work at first, check the allowScriptAccess attribute in you309.
import flash.external.ExternalInterface;
//send an alert
ExternalInterface.call("alert", "there is an alert");