in Flash, Programming

Using FlashVars with SWFObject in ActionScript3

I use SWFObject to add Flash elements to any given page that I work on containing Flash elements. SWFObject is one of the most elegant ways of handling Flash content. It uses a single div with basic CSS and a slim JavaScript file to:

  • determine if the user has Flash and if so, which version they are running
  • upgrade the user’s version of Flash with ExpressInstall (optional)
  • quickly pass in external variables (from your page) to your Flash file.
  • easily configure requirements, and if the user has Flash (or was just upgraded) the Flash content is displayed; otherwise, alternate text or images are used.

To pass variables into a Flash file using SWFObject you add the following line of code to your SWFObject javascript block:

soFlash.addVariable("msg", "this data came from an external source");

Now that a variable has been passed in via SWFObject, we can access it in Flash using ActionScript 3. If you’ve just started your first ActionScript 3 project (or you’re porting over AS2 code to AS3) and you’re scratching your head as to why your FlashVars are now undefined in the _root, it’s because they don’t live there anymore (in fact _root no longer exists! It is now just root without a leading underscore _).

The FlashVars are now available in the new LoaderInfo.parameters object. What you can do is create a single object that will store all external variables as properties of your object as demonstrated by the following code:

var allFlashVars:Object = LoaderInfo(this.root.loaderInfo).parameters;

// To access your variable names all you do now is refer to the
// variable name as a property of the object created above
// For example, to access an external variable named "msg" you would write:
// allFlashVars.msg;


Here’s the full code.

// All that is on stage is a TextField with an instance name of "output"
// Sure, we could have made this TextField using code, but the code might clutter
// what we're really here study.

// All of our external variables will be stored in this object
// Then we're going to loop over this object using a loop

var allFlashVars:Object = LoaderInfo(this.root.loaderInfo).parameters;

// To access your variable names all you do now is refer to the
// variable name as a property of the object created above
// For example, to access an external variable named "firstName" you would write:
// allFlashVars.firstName;
// For the purposes of this demonstration, we will output all of the external
// variable names and values

//Create a placeholder to keep the current variable name and variable value
var theVariableName:String;
var theVariableValue:String;

for (theVariableName in allFlashVars) {
theVariableValue = String(allFlashVars[theVariableName]);
output.appendText("\t" + theVariableName + ":\t\t" + theVariableValue + "\n");

//That's it! Happy coding!


Download the sample files (.zip)

Post any questions in the comments!

Write a Comment



  1. Man, thanks a lot for the tip! Now all variables in my movie are accessible! 🙂

    Now, time to discover how to make LightWindow call from inside the movie… wish me luck! 😛


  2. Glad you found the info useful! I’m not familiar with LightWindow but best of luck to you on the implementation! If you have any questions, just post them in the comments.


  3. Hi! have used something similar in an e-card movie, but can’t get it to work. Would love your feedback, can I send code?

  4. I looked for something that did this for ages. Thank you so much.
    Can you tell me how to access the individual variables that get loaded in? It may be a very basic thing (I appologise if it is) but I can’t seem to figure it out.
    Thanks again

  5. ok so if i am tring to pass a var like this:
    DL_AUTH_USERNAME on a sendVars object

    It is sent like this DL%5FAUTH%5FUSERNAME

    var dataOut:LoadVars = new LoadVars();
    function checkUser():Void {
    dataOut.DL_AUTH_USERNAME = loginForm.userName_txt.text;
    trace(dataOut); // is DL%5FAUTH%5FUSERNAME

    How do you get the _ to pass correctly? Thanks for any insight

  6. I am having a little trouble with the communication… Maybe you could look at some code and find my misstep. Thanks for the info, very helpful…


    var so = new SWFObject(“Hilti360.swf”, “Hilti360”, “540”, “360”, “8”, “#ffffff”);
    so.addVariable(“myUrl”, “index.html”);

    var xmlFile:String;
    xmlFile = root.loaderInfo.parameters.myUrl
    var myUrl:String;
    //var xmlFile:String;
    if (myUrl == “index-FR.html”) {
    xmlFile = “content/FR.xml”;
    } else if (myUrl == “index.html”) {
    xmlFile = “content/EN.xml”;
    } else {
    xmlFile = “content/PT.xml”;
    var xmlLoader:URLLoader = new URLLoader();
    var xmlData:XML = new XML();

    xmlLoader.addEventListener(Event.COMPLETE, LoadXML);

    xmlLoader.load(new URLRequest(xmlFile));

    function LoadXML(e:Event):void {
    xmlData = new XML(;

  7. @Josh: Your AS creates the string “xmlFile” which contains the value of the external variable “myUrl”. Your if statement then compares the value of a local variable named “myUrl”. As such, I’m assuming you’re always seeing “PT” in the output panel. So, I would think your code should look a little more like this (hope it helps!):

    //Create a reference to the xml file to be loaded.
    var xmlFile:String;
    //Store a reference of the external URL passed in.
    var myUrl:String;
    myUrl = root.loaderInfo.parameters.myUrl

    case “index-FR.html”:
    xmlFile = “content/FR.xml”;
    case “index.html”:
    xmlFile = “content/EN.xml”;
    xmlFile = “content/PT.xml”;

  8. Thanks a lot!, yet with this code:

    var xmlFile:String;

    var myUrl:String;

    myUrl = root.loaderInfo.parameters.myUrl;

    switch (myUrl) {
    case “index-FR.html” :
    xmlFile = “content/FR.xml”;
    case “index-PT.html” :
    xmlFile = “content/PT.xml”;
    default :
    xmlFile = “content/EN.xml”;

    I receive this output:

    …and only the default case is shown. Did I enter something wrong?

    Thanks Again!

  9. The first two traces display “null” because you are tracing the variables before any value is set.

    You are getting “undefined” in your third trace because you are testing within the Flash IDE. “myUrl” is passed in via JavaScript, which means you need to view this page in a browser so that you can pass the variable in via SWFObject.

    If you want to test within the Flash IDE, just check if myUrl is undefined and if so, set it to whatever you want.
    //Try something like this:
    var xmlFile:String;
    var myUrl:String;

    myUrl = root.loaderInfo.parameters.myUrl;
    //If you want to test within Flash, set a default value of
    //any external variables
    if(myUrl == undefined || myUrl == null){ myUrl = “index.html”}

    switch (myUrl) {

  10. That makes sense. I am still only able to show the default case, regardless of which html page a preview. Do I need to define something else in the html?


  11. Hey,

    how can i readout the parameters of a swf-file within an other Swf-FIle?


    SwfA loads SwfB and i wanna know, wich Vars are passed to SwfB.

    greets from Germany 😉

  12. Thanks for your tutorial. I dont understand how to make flash understand spaces in text.
    “Thisiswhatigot” and “This is what i need”

    my1.text = this.loaderInfo.parameters.my1;

    What should i add?

  13. Sorry! I found the solution! I didn’t emd font (space) for texteff=))
    But another one question, may be you’ll help=) if not! it’s ok! thanks a lot for you tutorial.
    Question: i have dynamic text area named my_txt.
    what should i write in actionscript to change fontcolore like i get content for text variable
    (my_txt.text.color = this.loaderInfo.parameters.my_txt_color;) – dont’t work
    or for movieclip tint color. something like this
    (i have movieclip named bg and i want to get tint color dynamicly from html)
    something like this:
    bg.tint = this.loaderInfo.parameters.bg_tint;
    (bg.tint = #ff0000) – for a example!
    Thanks a lot in any way!

  14. Georgiy:
    Good AS3 questions and very common for new developers. Here is how I would approach the tinting of a movieclip and textfield color….

    //To tint a movieclip dynamically in AS3:
    //(Assumes there is an instance on stage named "bg")
    var ct:ColorTransform = bg.transform.colorTransform;
    	ct.color = 0xA2C7F4;
    	bg.transform.colorTransform = ct;
    //To color text dynamically in AS3:
    //(Assumes there is an instance of a TextField on stage named "my_txt")
    //There are multiple ways of handling this, I just went with the easy solution of using the textColor property
    my_txt.textColor = 0xC5D9B7;

    Hope that helps and happy coding!

  15. This still helps today, Cheers! Most other examples look at the js side way to much and assume you can work the as3 stuff out.

  16. Hi this was very helpful! I do have a question:

    If I’m setting variables in the js using swfObject as you did in your sample, how can I make it so that flash reload those variables on a 5 second interval?

    I set up a timer in flash, which works, but only for the initial pull of the variables. after the timer launches the function that uses the variables loaded, it doesn’t update.

    This is the same issue I had using flashVars, my understanding was that swfObject removes the limitation of only loading the vars on document load. is this not the case?

    Thanks in advance!

  17. I get an error when I’m trying to implement it in my own swf.

    Error 1120: Access of undefined property output
    output.appendText(“t” + theVariableName + “:tt” + theVariableValue + “n”);

    I’m a noob 😎

  18. Hello,

    I just tried to use your script, but as soon as i add a TLF text into the scene, not linked at all to your script, just added, it breaks your script. Variables are not red anymore, they don’t appear in the TextField.

    I’ve spent 2 days trying to find the problem.


  • Audio-Player von aussen steuern? - Flashforum January 20, 2011

    […] […]