Exchange messages between Javascript and Flash

Updated . Posted . Visible to the public.

Flash movies (.swf files) can talk with Javascript code embedded in the same HTML page. There are two ways to do this:

  • The preferred way is to use the ExternalInterface Show snapshot class to call Javascript functions from ActionScript, and to bind ActionScript functions to the Flash movie's DOM element so they can be called from Javascript.
  • The deprecated way is to use the global fscommand Show snapshot function to call a Javascript function with a fixed name from Javascript. In return Javascript can sort of communicate with ActionScript by calling SetVariable(name, value) on the Flash movie's DOM element. This is super-legacy, but still encountered in the field.

Note that communication between a Flash movie and a piece of Javascript code is blocked unless they both live on the same domain. You can lift this restriction by adding a allowscriptaccess="always" attribute to the Flash movie's <embed> tag, and a <option name="allowScriptAccess" value="true" /> child tag to the Flash movie's <object> tag (which we keep around for Internet Explorer).

The attached link has more information.

Henning Koch
Last edit
Deleted user #4941
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2011-03-02 16:08)