-
Flash Design Guide
- Introduction To Flash
- Working with the Authoring Tool
- Working with Templates
- FreeHand for the Power User
- Creating Animation with Fireworks
- Creating Animation in Flash
- Preloading for Non-Programmers
- Drawing in Flash
- Using the Library
- Organizing Animations With Storyboards
- Working With Masks
- Making 2D Look 3D
- Working With Text
-
Flash Interactive Developer Guide
- What is an Interactive Developer?
- Creating Flash Projects
- Using Form Applications
- Working with Form Components
- Setting up a Movie for Design and Animation
- Setting up a Movie for Programming
- Working with Components
- ActionScript Fundamentals
- The Eolas Solution
- Intro to Flash Player 9
- Working with Strings
- Creating ActionScript Transitions
- Working with Boolean Objects in ActionScript
- Optimizing Delivery
- Working with Numbers in ActionScript
- Working with the Number Class
- Flash Professional 9 Preview
- Adobe Takes ActionScript Open Source
- Flash Rich Media Guide
-
Flash 8
- Introduction to Flash 8
- Advanced Text Rendering
- Using BitmapData Class in Your ActionScript
- The CacheAsBitmap Property
- Bitmap Rendering Improvements and Blend Modes
- Using The ExternalInterface Class
- Using Filters
- Downloading and Uploading Files with ActionScript
- Runtime Support for GIF and PNG Files
- Garbage Collection in Flash 8
- Using Enhanced Gradients
- Using Scale 9
- Stroke Enhancements in Flash 8
- Video Support in Flash 8
- How to Create a Slideshow
- Creating Dynamic Text in Flash
- Flash 3-D–Taking Flash to the Third Dimension!
- Object Collision Detection with Flash
- Create a Quiz With The Flash Quiz Template
- Working with Flash
- Introduction to Design in Flash
-
Developing with ActionScript
- Introduction to Programming within Flash MX 2004
- ActionScript Fundamentals
- Event Handling within ActionScript
- Creating Interactive Movies
- Testing your Flash Movies
- Debugging
- Using ActionScript to Control Your Movies
- Using ActionScript to Control Text
- The Differences Between ActionScript 1 and ActionScript 2
- Using Pre-Built Classes In Flash MX 2004
- Creating Your Own Classes
- Working with External Data in Flash
- Using Components
- Informit Articles and Sample Chapters
- Books and e-Books
- Online Resources
-
Rich Internet Applications
- Introduction to Rich Internet Applications
- Using Flash Variables
- Getting Data Into Flash: Loading External SWF and JPG Files
- Working With XML: What is XML, and How Does it Relate to Flash?
- Working with XML: Good XML vs. Bad XML
- Working with XML: Loading XML into Flash
- Working with XML — Dynamically Building XML with .NET, ColdFusion, Java, and PHP
- Working with XML — Web Services
- Working with Data — Working with XML
- Working with Data: Pushing Data back to the Server with Load Vars
- Working with Data: Leveraging Persistent Connections
- Flash Remoting
- Working with Data: Macromedia Flex Presentation Server
- Building Rich Internet Applications: Connecting Flash to a Database
- Building Rich Internet Applications: Planning, Planning, Planning
- Building Rich Internet Applications: Design Counts
- Building Rich Internet Applications: Beyond the Movie Clip
- Building Rich Internet Applications: Using Macromedia Central
- Building Rich Internet Applications: Using Macromedia Flex
- Presenting the Solution: Delivering Flash Applications to the Web
- Presenting the Solution: Delivering Flash Over Non-PC Devices
- Informit Articles and Sample Chapters
- Books and e-Books
-
Flash for Designers: Rich Media Production
- Graphic Control in Flash
- Introduction to the Rich Media Production
- Using Video, Audio and Images in your Flash Movies
- Using Components to Build Rich Media Solutions
- Controlling Components with ActionScript
- Using Audio in Flash
- Video in Flash
- Choosing Which Version of Flash Communication Server to Use
- Streaming Video
- Using the Microphone and Camera Core Classes
- Constructing Large Applications That Leverage Video and Audio
- Informit Articles and Sample Chapters
- Books and e-Books
- Matthew's Predictions for 2006
- Matthew's Favorite Flash Books
- Matthew's Favorite Flash Web Resources
- Macromedia as Part of Adobe
- First Look: Flash Player 8
- A Brief History of Flash
Using The ExternalInterface Class
Last updated Aug 22, 2003.
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:

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
ExternalInterface.call("f", txtOut.text);
}
Next, create an HTML page to which you can add some JavaScript. Create the basic page using this code:
<HTML>
<HEAD>
<meta http-equiv=Content-Type content="text/html; charset=ISO-8859-1">
<TITLE>External Interface example</TITLE>
</HEAD>
<BODY>
<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/
cabs/flash/swflash.cab#version=5,0,0,0"
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"
ALLOWSCRIPTACCESS="always"
TYPE="application/x-shockwave-flash"
PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer">
</EMBED>
</OBJECT>
<h4>External Interface: Hello World - Javascript side</h4>
<FORM NAME="inForm" METHOD="POST" ACTION="">
Receiving from AS<BR>
<INPUT TYPE="TEXT" NAME="inField" VALUE="No data received"><BR>
</FORM>
</BODY>
</HTML>
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:
<SCRIPT LANGUAGE=JavaScript>
// This function "f" will be called from ActionScript
function f(str)
{
document.inForm.inField.value = "AS says: Hello " + str;
}
</SCRIPT>
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> <INPUT TYPE="SUBMIT" VALUE="Send to AS"> </FORM>
The FORM action calls JavaScript. You need to add the following JavaScript to complete the communication:
function makeCall(str)
{
thisMovie("ExternalInterfaceExample").g(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.