Actionscript 3 Passing Variables from HTML to Flash (FlashVars, Query String)

Jan 17, 2011   Programming   Nick Vogt   Comments (1)
Please note that this post is over a year old and may contain outdated information.
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):

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

And now via the query string:

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

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)
Share This Post

Comments (1)

Dioris Moreno   Mar 25, 2013
Your explanation was very clear and helpful. Thank you.
Share This Post
H3XED © Nick Vogt   RSS   Policies   Twitter