Syndicate

Feed

Flash-Javascript Communication

This article tells the story of how Flash 8 movies learnt to fluently converse with Javascript using the ExternalInterface “API” in XHTML strict.

What the hell is that ?

The ExternalInterface is an ActionScript class that was introduced in Flash 8. It allows a Flash movie to communicate with the Flash Player container using ActionScript code. The Flash Player container is in 99.9% of cases a web page, hence the Flash movie is most often calling Javascript functions, either standard built-in Javascript functions or Javascript functions defined by us, usually in an external *.js file. Using the ExternalInterface class, we’re also able to let Javascript functions invoke ActionScript functions defined in the SWF movie. These Javascript and ActionScript functions can bear arguments and return values, and there can be as many of them existing on both sides of the communication bridge. Let’s keep that in mind.

When does it not work ?

The communication will fail when :

  • The Flash player used in the browser is older than version 8. There are other means of communication that work just fine in older Flash players, that we won’t cover in this article.
  • When we’re testing locally AND the Flash parameter allowscriptaccess is not set to “always”. (An exception to this is Firefox : testing locally with allowscriptaccess set to “always” won’t work, due to security reasons. In Internet Explorer, if you allow “active content” to be run locally, then you will not run into any problem.)

What’s the 6 million dollars challenge here ?

The biggest challenge is certainly to get a handle to the XHTML element in the Document Object Model (DOM) tree that stores the Flash movie. It’s through getting that handle (a handle to an element node) that we can open communication back and fro the Flash Player and our Javascript code. Most Flash books and the Macromedia documentation presents a quick and dirty way for getting such handle, under the cover of “you don't need to understand what's going on here”, a method based on the way Flash “objects" are usually embedded in HTML, using both the object and embed tags. This article provides an alternative method that will have as repercussions that 1- we won’t be shooting in the dark, not understanding what the hell we’re doing, and also that 2- our web page will validate as strict HTML or XHTML.

Last edited by Caroline Schnapp about 4 years ago.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <css> <html> <javascript> <mysql> <php> <span> <a> <b> <i> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <sup> <sub> <dd> <del> <blockquote> <img> <q> <p> <div>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <css>, <html>, <javascript>, <mysql>, <php>, <rails>, <ruby>.

More information about formatting options

CAPTCHA
I have to wonder if you're a human spammer or a machine, or less likely someone who cares to leave his or her thoughts behind.