Archive
This post is archived and may contain outdated information. It has been set to 'noindex' and should stop showing up in search results.
Actionscript 3 Passing Variables from HTML to Flash (FlashVars, Query String)
Jan 17, 2011ProgrammingComments (1)
Passing variables to a Flash movie that is embedded in a web page can be done using either the query string or FlashVars. The difference between these methods is that using query string will cause browsers to re-download the Flash movie each visit, while FlashVars allows browsers to use the cached version. Depending on the functionality you want, one method may suit you better than the other.

Here is an example of loading variables via FlashVars (use an & to separate each name/value pair):

<object>
<param name="movie" value="movie.swf">
<param name="FlashVars" value="name1=value1&name2=value2">
<embed src="movie.swf" FlashVars="name1=value1&name2=value2">
</object>

And now via the query string:

<object>
<param name="movie" value="movie.swf?name1=value1&name2=value2">
<embed src="movie.swf?name1=value1&name2=value2">
</object>

Notice that both methods require you to use redundant param and embed tags. This is to solve browser compatibility issues.

Once you have passed variables to the Flash movie, you can retrieve them in Actionscript 3 using root.loaderInfo.parameters. If you know the "name" of the variable you want to retrieve, you can use a simple String variable to load it like so:

var var1:String = root.loaderInfo.parameters.name1;
Using the above code, var1 is assigned the string value1 and you can then use it like you would a normal string.

If you're passing lots of name/value pairs into Flash, or you're passing them dynamically, you can instead load them all into an object, and then loop through it like this:

var vars:Object = root.loaderInfo.parameters;
for(var each:String in vars)
{
vars[each];
}
Comments (1)
Add a Comment


Please review the commenting policy prior to commenting.
Dioris Moreno   Mar 25, 2013
Your explanation was very clear and helpful. Thank you.