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.
For information about the basics of using the <OBJECT> tag, see OBJECT tag syntax.
Note: The FlashVars feature requires Flash Player 6 or later.
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."
Object tag example
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" id="myFlashMovie" align="middle"> <param name="movie" value="myFlashMovie.swf" /> <param name=FlashVars value="myVariable=Hello%20World&mySecondVariable=Goodbye" /> <!--[if !IE]>--> <object type="application/x-shockwave-flash" data="myFlashMovie.swf" width="550" height="400"> <param name="movie" value="myFlashMovie.swf" /> <param name=FlashVars value="myVariable=Hello%20World&mySecondVariable=Goodbye" /> <!--<![endif]--> <a href="http://www.adobe.com/go/getflash"> <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /> </a> <!--[if !IE]>--> </object> <!--<![endif]--> </object>
Embed tag example
The <EMBED> tag can also be used to add a SWF file to a web page, but it is an older tag and now obsolete.
<embed src="myFlashMovie.swf" quality="high" bgcolor="#ffffff" width="550" height="400" name="myFlashMovie" FlashVars="myVariable=Hello%20World&mySecondVariable=Goodbye" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflash" />
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.
- Uses a for..in loop to iterate over each of the passed variables.
- Displays the variables names and values in the text field.
// AS3 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"); try { var keyStr:String; var valueStr:String; 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 } } catch (error:Error) { tf.appendText(error.toString()); }
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.
- For an example of the query string technique, see: Using FlashVars with ActionScript 3.0 (Adobe Blogs).
- For information about the other attributes you can use with the OBJECT tag, see Flash OBJECT and EMBED tag attributes.
Keywords: pass; parameters; query string; object; embed; tn_16417