PrimitiveType

Communication between Flash and JavaScript


As there is a growing demand for media-rich Flash content alongside more accessible HTML content, it is sometimes desirable to be able to merge the two in one page or application. There are a number of ways that JavaScript and Flash can be used in tandem to achieve this.

Although most users have Flash installed on their browsers, some don't and some have old versions. You can use JavaScript to ascertain if Flash is installed and what version of Flash is installed, so that your site can deal elegantly with users lacking a specific Flash plugin. There is no built-in function for this, but it can be figured out by examining the navigator.plugins array if it is available, or the properties of an instantiated Flash ActiveX object otherwise.

Flash can also communicate with JavaScript, though in a restricted way. For example, you can call a JavaScript function by using ActionScript's getURL(), passing a "javascript: doSomething()" type of argument.

A more formal way of communication between the two languages is through FSCommand (which also has other uses and has been available since Flash Player 3). Using FSCommand, JavaScript can call a limited set of methods belonging to Flash objects. A Flash movie can also communicate with JavaScript by means of FSCommand. However, the support for FSCommand is not universal and may require additional modules such as LiveConnect.

Because FSCommand doesn't work on all platforms/browsers, a Flash/JavaScript Integration Kit was created to allow communication both ways between JavaScript and Flash. The kit is free and open source. Installation of the kit requires more work than using the above two methods, but once installed, the communication between JavaScript and Flash has the additional benefit that it is not limited to strings, as it can also handle complex data types such as arrays and objects.

The Flash/JavaScript Integration Kit is not a perfect solution. It has been tested on several browsers, but there are older browsers that may not be compatible. Therefore, an application that makes use of it will still have to check for browser versions.