FireworksColdFusionDreamweaverFreehandFlashMXHome
Latest New Content

Latest Free Content
View All
Free Content
Accessibility
CMX Learning Guides
Hosted by enterhost

JavaScript Integration in Flash 8

By: Danny Patterson

Page 1 of 3

Set for printing

Next

Flash's ability to integrate with JavaScript just took a huge leap forward with the release of Flash 8. Introducing the External Interface! This is a new feature in Flash 8 that allows for better communication between Flash and its host. Most of us will use this for integration with JavaScript. This article will show you how easy this new feature is to use. It will whet your appetite and get you thinking about how you want to use this in your applications.

The Flash 8 authoring tool has not been released at the time of this writing. It is set to be released sometime in September 2005. To view the examples shown in this article, you will need to install the Flash Player 8 public beta.

Out With the Old

In previous versions of Flash, JavaScript communication was done via fscommand. This was a buggy and complex technology. In fact, it was so bad that most developers actually preferred to put their JavaScript function calls inside the getURL function. This got the job done, but was far from ideal.

Mike Chambers and Christian Cantrell released the Flash/JavaScript Integration Kit as a better way to handle communication between the two technologies. This made the integration much easier to implement, but was still built on the same old technology. What we really needed was a new technology.

In With the New

Flash 8 introduces a ground-breaking new way to integrate Flash with its host called the External Interface. This allows Flash/JavaScript integration to be more powerful and stable. It is also very easy to use. External Interface offers Flash 8 Developers the following advantages: it's easier to implement, it allows for synchronous communication, and it supports sending a wider selection of data types, including objects.

External Interface is extremely easy to implement. As you will see in the examples below, only a few lines of code are needed on either the Flash or JavaScript side. Fscommand required a large amount of JavaScript/VBScript on the host page just to call a JavaScript function. The only code required on the host page for Flash to call a JavaScript function is the function itself. That's right, Flash can call any JavaScript function on the SWF's host page without any additional JavaScript code. Going from JavaScript to Flash is also very simple. All you need to do in JavaScript is get a reference to your HTML object or embed tag (depending on the browser) and call the ActionScript function. Inside the Flash application, you simply need to make that function available to JavaScript by calling the addCallback function of the External Interface class. More on this below.

Fscommand didn't allow for synchronous communication. Therefore, to make a round trip, Flash would need to call a JavaScript function, the JavaScript would need to turn around and use SetVariable to create a variable in the Flash movie. All this time Flash was checking to see if that variable was set. This communication is far from ideal. The External Interface allows you to call a JavaScript function and receive a return value. This simplifies the required logic and should open the door for more complex applications.

Fscommand allowed you to send primitive data types as arguments in the function calls. Now, with External Interface, you can send complex objects as arguments. In example 2 below, we are passing the entire system.capabilities object back to JavaScript.

I know what you're thinking: "External Interface is great, but does it work in all the browsers?" The following is a list of supported browser versions and platforms (and yes, it is Flash 8-only):

Third-party projector tools use fscommand to allow Flash to communicate with the container application. This will also be replaced by the new External Interface. Since External Interface allows for sending objects and returning values, we can expect a huge increase in the stability and functionality of these projector tools.

Page 1 of 3 1 2 3 Next


download
Download Support Files


Keywords
ExternalInterface, External, Interface, sneak peek, Flash 8, fscommand, javascript, integration, communication