Home / Articles / Adobe Creative Suite / External Communications with Flash 8: How to Use the ExternalInterface

External Communications with Flash 8: How to Use the ExternalInterface

Article Description

Kris Hadlock explores the use of the ExternalInterface class in Flash 8. With ExternalInterface, you can build robust object-oriented Flash applications that can be connected to the outside world and tied to databases and other server-side processes.

Like this article? We recommend

Macromedia Flash 8: Training from the Source

Macromedia Flash 8: Training from the Source

$44.99

The ActionScript Connection

Creating the connection between ActionScript and JavaScript is very simple. The first thing that you need to do is import the ExternalInterface class by adding the following code at the top of the chart class:

import flash.external.*;

To create the callbacks that will allow JavaScript to call ActionScript methods, we need to use the addCallback method. This is a static method of the ExternalInterface class that takes three parameters. The first parameter is a string that represents the name of the method that JavaScript will call. The second method is the object instance to which you’re scoping the call. The third parameter is a reference to the method that you’re calling in that object instance. Following is the code used in the sample:

ExternalInterface.addCallback("highlightItem", this, highlightItem);
ExternalInterface.addCallback("addAllItems", this, addAllItems);

In this example, the first parameter is the name of the method that JavaScript calls; the second has a value of this, which is the scope of the chart class; last is the name of the actual method in the chart class. This code is called in the Charts constructor function to create the connection before other code is instantiated.

After we add the callback functions, we need to make the call to JavaScript to tell Firefox that the chart is loaded. Use the call method and pass the name of the method that we created in JavaScript as a string:

ExternalInterface.call("onFlashLoaded");

Again, when this method is triggered in JavaScript, it will return an array full of lender names and rates to create the bars in the chart.

6. Handling Unsupported Browsers | Next Section Previous Section