Actionscript 3 Passing Variables from HTML to Flash (FlashVars, Query String)
Posted January 17, 2011 by Nick Vogt in Programming
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];
}

Comment on this post


Legacy Comments (2)

ezell | August 10, 2011 | 6:43 PM PST
Thanks after trying to figure out how worldstarhiphop was embedding their player final figured out they were using mod-rewrite that pointed to a url that passed in values like this
Chris | April 29, 2011 | 3:47 PM PST
Fantastic! Thank you!!!!!!!!!!!
Features
Free Web MP3 PlayerComputer Build GuidePHP Beginner Tutorials
Post Series
ActionScript 3 TutorialsHard Drive Cost Charts
Popular Tags
actionscriptajaxcall of dutycrysisebayfacebookgooglejavascriptminecraftneweggphprageskyrimtutorialyoutube


H3XED © 2012 Nick Vogt | Web Design
Saturday, May 19, 2012 | Privacy Policy | Disclosure Policy | Contact