Chapter 22. Building Integrated Applications

Introduction

The ExternalInterface class allows the Flash Player to communicate in a synchronous manner with the application within which it is embedded. In many cases that host application is a web browser. As such, this chapter focuses exclusively on how to build applications that integrate ActionScript and JavaScript when Flash Player is embedded in a browser.

Calling JavaScript Functions

Problem

You want to call a JavaScript function from ActionScript.

Solution

Use ExternalInterface.call().

Discussion

Use the ExternalInterface.call() method to make synchronous calls to JavaScript functions from ActionScript. The call() method requires at least one parameter as a string specifying the name of the function to call:

ExternalInterface.call("changeTitle");

The function must be defined in the HTML page with the same name:

<script language="JavaScript">
    function changeTitle(title) {
        if(title == undefined) {
            title = "New Title";
        }
        window.title = title;
    }
</script>

If the JavaScript function accepts parameters, you can pass values to it by adding additional parameters when calling the call() method. For example, the following passes a value to the changeTitle() function:

ExternalInterface.call("changeTitle", "ActionScript 3.0 Cookbook");

Since call() is synchronous, any values returned by the JavaScript function are immediately returned to ActionScript. That means you can assign the return value from a call to a variable. The following ActionScript illustrates ...

Get ActionScript 3.0 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.