Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > Adobe Flash

Flash Reference Guide

Hosted by

Toggle Open Guide Table of ContentsGuide Contents

Close Table of ContentsGuide Contents

Close Table of Contents

Using The ExternalInterface Class

Last updated Aug 22, 2003.

If you’re looking for more up-to-date information on this topic, please visit our Flash article, podcast, and store pages.

Over the last few years, Flash has used techniques such as FSCommands, FlashVars, and even a few LoadVars tricks to communicate to the Web browser through JavaScript. Despite these workarounds, communication between Flash and the Web page has never been easy. Enter the ExternalInterface class.

The ExternalInterface class, a brand new class within the Flash Player, allows you to more easily connect Flash with a Web page. The best part? ExternalInterface is not restricted to communicating with JavaScript. You can use it to communicate with VBScript in a Web page or even have the Flash movie communicate with programming languages such as C# if the Flash movie is placed within a Windows Form.

On the whole, however, most developers will use ExternalInterface to communicate Flash to a Web page and have the Web page communicate back via JavaScript. In fact, I'm betting it won't be long before the ExternalInterface class will become more commonly known as the JavaScript API.

You will be delighted to know that using the ExternalInterface class is quite straightforward. All you need is Flash, a Web page to which you can add some JavaScript, and a Web server to host your final pages. A word of caution at this point: the Flash Player has security that prevents the ExternalInterface Class from executing unless the Web page and SWF are running from a Web server.

There are three methods for the ExternalInterface Class that allow you to call an external interface (such as JavaScript), have the external interface call back (such as JavaScript being able to talk to Flash), and check whether or not you can communicate with JavaScript.

Checking Whether Flash Can Communicate

Let's start by checking to see whether or not you can communicate with JavaScript. This is useful in case the user has turned off JavaScript support in his Web browser.

The first step is to drag a Label component into the Library. This component will tell you whether or not JavaScript can be communicated to. Add the following JavaScript to the Layers panel:

import flash.external.*;
_root.attachMovie("Label", "lbl", 20);
lbl._x = 15;
lbl._y = 15;
lbl.text = "";
lbl.autoSize = "left";
var isAvailable:Boolean = ExternalInterface.available;
lbl.text = isAvailable;

The first line of the ActionScript allows Flash to import the ExternalInterface classes into the SWF. The next five lines of code are for the Label component. The final two lines call the ExternalInterface class to see if it is available. The "available" property has a true or false value. The results are then sent to the Label component.

You'll need to publish your files and move the SWF and HTML page to your Web server. From there you can view the page. It should look something like this:

Figure 091605

I know this is not very exciting visually. But it gets better.

Communicating FROM Flash to a Web Page Through JavaScript

Now, let's make Flash talk to a Web page. You will need to add a TextInput and Button component to the Library. In Flash, add the following ActionScript to place the objects correctly on the Stage:

Now, you need to write a function in ActionScript that will use the ExternalInterface class to communicate with JavaScript:

sendButton.onRelease = function() {
  // Call Javascript function f"f", txtOut.text);

Next, create an HTML page to which you can add some JavaScript. Create the basic page using this code:

 <meta http-equiv=Content-Type content="text/html; charset=ISO-8859-1">
 <TITLE>External Interface example</TITLE>
 <OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
     WIDTH="400" HEIGHT="300" id="ExternalInterfaceExample">
  <param name="allowScriptAccess" value="always" />
  <param name="movie" value="ExternalInterfaceScript.swf" />
  <param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />
  <EMBED src="ExternalInterfaceScript.swf" quality=high
      bgcolor=#CCCCCC WIDTH="400" HEIGHT="300"
      NAME="ExternalInterfaceExample" swLiveConnect="true"

 <h4>External Interface: Hello World - Javascript side</h4>
 Receiving from AS<BR>
 <INPUT TYPE="TEXT" NAME="inField" VALUE="No data received"><BR>

You should have a simple page that has the Flash movie embedded to it through the OBJECT tag. Now, in the TITLE section of the page you need to add the following JavaScript:

  // This function "f" will be called from ActionScript
  function f(str)
    document.inForm.inField.value = "AS says: Hello " + str;

The "f" string binds the data between the two. Run the movie from your Web server and you'll be able to send a message from Flash to the Web page via JavaScript.

Communicating TO Flash From the Web Page Via JavaScript

Now, you're going to reverse the process. Add the following additional objects to your Flash movie:

_root.attachMovie("Label", "lblJs", 50);
lblJs._x = 15;
lblJs._y = 120;
lblJs._width = 150;
lblJs.autoSize = "left";
lblJs.text = "Receiving from Web page";

_root.attachMovie("TextInput", "txtIn", 60);
txtIn._x = 15;
txtIn._y = 150;
txtIn._width = 150;
txtIn.editable = false;

You'll also have to use a function in Flash to receive a request sent to it from the Web page. This is the function you can use:

function g(str:String) {
  txtIn.text = "JS says: Hello " + str;

// Make function g available to Javascript
ExternalInterface.addCallback("g", this, g);

To allow JavaScript to communicate back to Flash you need to add some additional HTML objects to the HTML code above. The code is:

<FORM NAME="outForm" METHOD="POST" ACTION="javascript:makeCall(document.outForm.outField.value)">
 Sending to AS<BR>
 <INPUT TYPE="TEXT" NAME="outField" VALUE="Enter your name"><BR>

The FORM action calls JavaScript. You need to add the following JavaScript to complete the communication:

  function makeCall(str)

Now, you can test your movie and Web page. You should be able to chat back and forth very easily. What you have worked through in these examples is only a taste of what you can do with the ExternalInterface class.

Why Communicate With JavaScript?

A number of years ago, Macromedia tried to convince the world that Flash would replace HTML. The reality, however, is that there are simply too few Flash ActionScript developers to displace the massive number of Web developers and tools. Today's reality is that Flash is merely a part of the overall Web site experience.

Web pages do a lot of things and Flash does a lot of things. Coupling the two gives you the best of both worlds. This is where the ExternalInterface class comes into play. Consider, for instance, if you were to create an AJAX Web application (AJAX is Asynchronous JavaScript and XML). Why not mix Flash and JavaScript together? By leveraging Flash's ExternalInterface class you can have the two technologies easily communicate with each other. Google's Earth project is a great example of this.

Today, the Google Earth project is one of the best examples of an AJAX application. Google, however, has had problems rendering the graphics in its Google Earth AJAX application. To resolve this issue, Google created a version of Google Earth specifically tailored for different Web browsers. This is where Flash using the ExternalInterface class can help. Instead of having multiple code bases, why not use Flash with ExternalInterface to handle the graphics for all browsers?

With your Flash movie tightly coupled to the Web application, you don't have to think of building either Flash or multiple AJAX sites. What you need to think about is how you can build hybrid Flash/AJAX sites. Maybe we could call this new technology F-JAX (Flash-JavaScript and XML)? You heard it here first, folks.