Accessibility

Table of Contents

Using the External API for Flash–JavaScript Communication

ExternalInterface.call()

ExternalInterface.call() can invoke a JavaScript function and handle a return value. In the past, you had to use one method to invoke a JavaScript function and then a different method to receive a return value. Now you can receive a return value immediately by invoking just one function. So much easier!

The ExternalInterface.call() method uses the following basic format:

ExternalInterface.call(methodName:String, [parameter1:Object])

The parameters have the following meanings:

  • MethodName: The name of a JavaScript function to call
  • parameter1: Any parameters to be passed to the function; you can specify zero or more parameters, separating them by commas

Type the following code in Flash:

import flash.external.*;
// The name of a JavaScript function to call 
var callJasFunction:String = "callJavascript";
//parameter 
var msg:String = "Hello. ^^";
// The return value after calling JavaScript 
var returnValue:String = ExternalInterface.call(callJasFunction, msg).toString();
return_txt.text = returnValue;

Type the following JavaScript code:

<script language="JavaScript">
    function callJavascript(str) {
    alert(str)
    return "ExternalInterface is a new Flash Player 8 API."
    }
</script>

When callJavascript() is invoked, an alert window pops up indicating that the value has been received by your JavaScript function. When you click the OK button, a return value is sent to Flash Player, which receives the return value and displays it in return_txt.text (see Figure 5).

Value returned from JavaScript

Figure 5. Value returned from JavaScript

Note: As usual, when testing locally, change allowScriptAccess="sameDomain" to allowScriptAccess="always" in Flash Player 8. Also, to let your local Flash content communicate with the Internet, read this Flash Player TechNote.