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.
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.
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.
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.
Uses a for..in loop to iterate over each of the passed variables.
Displays the variables names and values in the text field.
var tf:TextField = new TextField(); // create a TextField names tf
tf.autoSize = TextFieldAutoSize.LEFT;
tf.border = true;
addChild(tf); // add the TextField to the DisplayList so that it appears on the Stage
tf.appendText("params:" + "\n");
var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters; //set the paramObj variable to the parameters property of the LoaderInfo object
for (keyStr in paramObj)
valueStr = String(paramObj[keyStr]);
tf.appendText("\t" + keyStr + ":\t" + valueStr + "\n"); // add each variable name and value to the TextField named tf
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.