Introduction

The FlashVars parameter of the HTML <OBJECT> tag sends variables into the top level of a SWF file when it loads in a web browser. The <OBJECT> tag is used to add SWF files to HTML pages. The <EMBED> tag can also be used, but is older and now obsolete.

Use FlashVars to pass values to a SWF file

You can use an optional parameter named "FlashVars" to pass variables into the root level of the SWF file. Place this parameter within the <OBJECT> tag or the older <EMBED> tag. All of these variables are then passed to the SWF file before the first frame of the file is played.

You can pass as many variables as you want with any variable names that you want. All browsers support FlashVars strings of up to 64 KB (65535 bytes) in length.

The format of the FlashVars property is a string that is a set of name=value pairs separated by the '&' character.

You can include special or nonprintable characters by using a '%' character followed by the appropriate two-digit hexadecimal value. A single blank space can be represented using the '+' sign. For more information about URL-encoding special characters, see URL Encoding (Wikipedia).

The following HTML code example passes two variables. The first is called "myVariable" with a value of "Hello World". The second is called "mySecondVariable" with a value of "Goodbye."

The browser encodes the FlashVars string the same way it encodes the rest of the HTML page. Internet Explorer on Windows provides UTF-16 compliant encoding. Other browsers provide UTF-8 compliant encoding.

Access the variables in ActionScript 3.0 code in a SWF file

Once you have passed variables to your SWF file from the HTML, the SWF file requires to use of those variables.

Use the ActionScript 3.0 LoaderInfo object to access the FlashVars variables.

Simple example

The code example below queries the LoaderInfo object to access the variable named myVariable passed to the SWF file in the FlashVars parameter. Then it sets the text of a text field on Stage to the string value of the variable.

// by querying the LoaderInfo object, set the value of paramObj to the
// to the value of the variable named myVariable passed from FlashVArs in the HTML
var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters.myVariable;
// set the text of the text instance named text1. Use the toString() method
// to convert the value from an object to a string so it can be used in the text field
text1.text = paramObj.toString()

You can download a sample FLA, SWF file, and HTML file showing the above code in use.

Advanced example

The code example below does the following:

Creates a TextField instance on the Stage (named tf).

Uses the LoaderInfo object to pull in the variables from the FlashVars parameter.

Access the variables in ActionScript 2.0 code in a SWF file

In ActionScript 2.0, you can access the FlashVars variables directly, as if they had been declared in the SWF file itself.

This example sets the text property of a dynamic text instance called text1 to the value of the variable called myVariable. myVariable is declared in the FlashVars parameter.

// AS2
text1.text = myVariable;

You can download a sample FLA, SWF file, and HTML file showing the above AS2 code in use.

Additional techniques

Variables can also be passed to SWF files by appending them to the URL of SWF files specified in OBJECT and EMBED tags. In this technique, the additional information appended to the URL is called a query string. This method has limitations if you need the variables immediately when the SWF file loads. When you use this method, it's necessary to download and play the SWF file before large amounts of data can be requested from the server.