ActionScript
Passing parameters to a Flash movie

Generic Flash objects typically need one or more parameters to function. When displayed via a web page, there are 3 main methods to accomplish this. Normally, I then use these parameters to read a configuration file and/or to query a database and get more information.

This page covers what works, what doesn't, and presents some related notes.

URL Parameters | FlashVars Parameters - Notes Fails Debug Code
ExternalInterface class - Calling Javascript | Called by JavaScript | IE Failures | fscommand | SetVariable
Multiple Flash Applications


URL Parameters

When calling web servers (cgi and ISAPI executables), it is common to pass named parameters via the hyperlink. The question mark (?) indicates the beginning of the parameters, individual parameters are separated with ampersands (&). The help says that this same technique can be used with Flash movies. Parameters passed this way are accessed using I could not get this method to work.


FlashVars Parameters

Assuming that you are launching the Flash movie via the Adobe provided code, you can pass parameters via the FlashVars parameter.

The basic html code (but without FlashVars) is automatically generated via

which also creates AC_RunActiveContent.js. Note that, when using the default settings, the Flash file, html file, and AC_RunActiveContent.js should be copied to the same server directory.

To pass parameters, modify the html file to add FlashVars to both the AC_FL_RunContent function

and the object definition Notice that the object definition requires 2 entries of FlashVars

whereas AC_FL_RunContent only requires one and creates the correct format based on the detected browser type.

Parameters passed this way are accessed in AS3 using any of the following

A number of characters have special meanings and can not be directly entered in names or values ... these will need to be entered as hex values. The following are some of most common problem characters.

The following will pass a filename with query parameters. Without the hex encoding, this parameter would be broken up into several separate parameters. Also note that the parameter names are case sensitive.


Notes

As usual, The Adobe Flash 9 (AS3) help was completely worthless.

After stumbling around for a while, I discovered the following hint in the help for LoaderInfo.parameters.

Unfortunately, there are only two (2) occurrences of FlashVars in the ActionScript 3.0 sections of the help and no explanation of what it means.

It took searching the internet to get any real information. Once I found an example - search the page for FlashVars - the rest was fairly straight forward.

After creating this page, I discovered that the Flash ActionScript 2.0 help contains several examples. I never thought to look there because most of the AS 2.0 help just gets in the way ... which is why I typically keep it disabled. There are 2 sections

(The links go to the associated web pages which also contain user notes.) In the help, you will need to click (expand) the plus sign to see the examples ... on the web pages, the examples are already expanded. Notice that the way to access the parameters is different - the method I show above works for AS3, the help files show the AS2 method where variables with the correct names are automatically generated.

Also note that the AS2 help suggests using both FlashVars and LoadVars to get data ... but the AS3 help says that LoadVars is no longer used. This is why I leave the AS2 help turned off and don't bother testing its code examples.


Fails

Based on the help, and the lack of AS3 FlashVars examples, these represent how I interpreted the help. These were tested and do not work. And It was at this point that I searched the internet.


Debug Code

This is the debug code I used this to figure out what actually works. Filename_UIText was a Text field on the form. I also comment out the <noscript> tag (by changing it to <!noscript> so that I can test both the JavaScript and the object code - this shows 2 copies of the same Flash movie.


ExternalInterface class

The ExternalInterface class allows ActionScript 3 to interface with Javascript functions located in a web page. The provided help provides pretty good examples showing both (See Example: Using the external API with a web page container)

The examples below require that ScriptAccess is enabled.

The AS3 help shows where these go in the html file.


Calling Javascript

This goes in the Flash program And the matching javascript code (in the related html file)


Called by JavaScript

This goes in the Flash program In the web page (works in IE 6, fails in FireFox 1)


Multi-Browser

When writing JavaScript, you have to realize that there are no reasonable standards. Each browser follows what ever rules it wants. As a result, the code in the help files fails on Netscape browsers and Macintoch systems.

I found the following code as a user comment on the Adobe web site. I was able to verify that it works with with IE 6 and FireFox 1.

So this is what I am currently using


IE Failures

To call ActionScript functions, an alias is defined and JavaScript "calls" (references) the alias. The problem is that some alias names are not supported ... and, of course, The following aliases are known to fail with Internet Explorer (IE) - play, stop, LoadMovie - note that Load_movie works.

When using LoadMovie, double clicking the Error on page warning (in the status bar) displays this totally unhelpful error.

When the name used in the JavaScript does not match the alias name, this error will be displayed. Notice that the line number changed ... same code different line number ... very confusing.

play and stop generate

When testing these types of failures, beware of the various caches - your changes my not show up for several hours. Be sure to open a new IE window to load the most current swf file (Refresh - F5 - uses the cached version) - I use ctrl-n (New Window) with Internet Explorer.

Another failure occurs if you try to display the html file by simply double clicking it.

The message goes on to tell you how to change your security settings so this warning will not be displayed - don't do that, it will open your machine to attacks from other Flash applications. Since there is no failure when getting the page from the web, my suggestion is to open the html page using a local web server.


Old Methods - fscommand

The old way to access Javascipt was to use fscommand However, the AS3 (Flash 9) help says


Old Methods - JavaScript "SetVariable"

According to Adobe, using Flash 4 it is possible for JavaScript on an html page to change values in a Flash movie. The basic method is I have not tested this. It seems unlikely that this is useful. For one thing, it does not work with Macintosh. In addition, I don't know how the Flash application will know that there has been a change ... I guess the value could be checked with a timer.

Also, if you called a JavaScript function, it could set multiple Flash variables before returning.


Multiple Flash Applications

When 2 or more Flash applications are running on the same computer, they can communicate using the LocalConnection class.

These applications can be in the same web page, or not. This technique can be used to develop popup windows to change parameters.


Author: Robert Clemenzi - clemenzi@cpcug.org
URL: http:// cpcug.org / user / clemenzi / technical / Languages / ActionScript / Getting_html_data.html