Robots Rule | MindSculpt.net

New media has met its match.

The MindSculpt Design Syndicate focuses on code that works, from CSS, XHTML and JavaScript tips and tutorials to Flash and ActionScript how-to's. Robots not included.

Simple approaches to grabbing flashvars from HTML in Flash AS3

Michael Becker

Michael has humbly acquired extensive design experience across multiple industry disciplines during his career in new media. In addition to his passion for web and Flash development, he has an uncanny penchant for zombies and robots. Let's be honest, who doesn't?

The following article assumes you have a solid understanding of Flash AS3 and the Flash interface and skips all basic Flash setup instructions and explanations in effort to provide you with quick, concise information.

When using flashvars locally, I used to create a temporary variable that holds the information needed to run the application offline. That way I could test it locally, and comment it out when it was ready to be pushed live. Here’s my original approach:

  1. // function to retrieve any flashvars set in the html document
  2. function getFlashVar(_var:String):String {
  3.    var paramObj:Object = LoaderInfo(this.loaderInfo).parameters;
  4.    var flashvar:String = String(paramObj[_var]);
  5.    return flashvar;
  6. }
  7. // variables to store flashvars values grabbed from the html ›› uncomment for live site
  8. // var myFlashVar:String = getFlashVar("testFlashVar");
  9. // for testing (comment out for live site);
  10. var myFlashVar:String = "testFlashVar";

The problem with this technique is that it’s extremely easy to forget to comment/uncomment the testing/live variables before handing it off to your technical lead or publishing it to your site. Forgetting to reset the values obviously leads to broken flash files and angry techies.

Let’s avoid all of that by taking an extra step and setting up a conditional statement that checks to see if a flashvar exists; if it doesn’t exist, use the default value. The new approach looks like this:

  1. // function to retrieve any flashvars set in the html document
  2. function getFlashVar(_var:String):String {
  3.    var paramObj:Object = LoaderInfo(this.loaderInfo).parameters;
  4.    var flashvar:String = String(paramObj[_var]);
  5.    return flashvar;
  6. }
  7. // set a variable to hold the value of our flashvar
  8. var myFlashVar:String;
  9. function checkForFlashVars():void {
  10.    if (myFlashVar == null) {
  11.       // the flashvar doesn’t exist ›› assign a default value so we can publish and view the file locally
  12.       myFlashVar = "testFlashVar";
  13.       trace("flashvar is not present…using default value: " + myFlashVar);
  14.    } else {
  15.       // flashvar exists ›› get the flashvar from the HTML
  16.       myFlashVar = getFlashVar("testFlashVar");
  17.       trace("flashvar is present: " + myFlashVar);
  18.    }
  19. }
  20. // call the function
  21. checkForFlashVars();

Copy and paste the above into a blank AS3 document, then publish it and you’ll see it runs off of the default value (output in the trace). Using this approach when flashvars are correctly configured in the HTML saves time, confusion and sanity. If you’re wondering why I’ve encapsulated the conditionals into the checkForFlashVars() function, it’s mainly for personal preferences. This way, if I need something else to happen before the flashvars calls (or more likely, after the flashvar values have been pulled in), I can place checkForFlashVars() anywhere I need to ensure nothing in my application fires until the values (or lack of values) have been detected and stored.

If you need to handle the values of multiple flashvars, a similar approach can be taken using an object to load and store the parameters (flashvars) passed in from the HTML:

  1. // set a variable to hold the value of our first flashvar
  2. var myFlashVar:String = "testFlashVar";
  3. // set a variable to hold the value of our second flashvar
  4. var myFlashVar2:String = "false";
  5. // set an object to hold all of the parameters (flashvars) passed in from the html
  6. var paramObj:Object=LoaderInfo(this.root.loaderInfo).parameters;
  7. function checkForFlashVars():void {
  8.    // check to see if myFlashVar has a new value passed in from the html; if it does, use this value instead
  9.    if (paramObj.xmlpath) {
  10.       myFlashVar = String(paramObj.myFlashVar);
  11.       trace("flashvar is present: " + myFlashVar);
  12.    }
  13.    if (paramObj.myFlashVar2) {
  14.       myFlashVar2 = String(paramObj.myFlashVar2);
  15.       trace("flashvar is present: " + myFlashVar2);
  16.    }
  17. }
  18. // call the function
  19. checkForFlashVars();

If you copy and paste this code into a blank AS3 document, you’ll notice no trace codes in the Output Panel. That result means no flashvars are present, and the default values for testing are being used. If you need to grab additional flashvars, duplicate the variables/conditionals above. Based on your programming preferences, a great way to organize multiple conditionals is to use a switch() statement. Either one of the above flashvars techniques can be customized and extended as needed.

Lee Brimelow outlines a simple approach on his Flash website:

  1. // set a var to hold the value of the flashvar
  2. var myFlashVar:String;
  3.  
  4. // if the flashvar exists…
  5. if(root.loaderInfo.parameters["isLive"] != null)
  6.  
  7.    // set our temp value equal to the flashvar embedded in the HTML
  8.    myFlashVar = root.loaderInfo.parameters["isLive"];
  9.  
  10.    // trace out the value
  11.    trace("myFlashVar: " + myFlashVar)

…where “isLive” is a flashvar set in the swfObject in the HTML.

As with all programming techniques, there are many approaches that can be applied so solve the similar problems. For me, these are the most comprehensive and most reliable methods I’ve found.

Share this post:
  • Twitter
  • Facebook
  • del.icio.us
  • Digg
  • LinkedIn
  • Reddit
  • StumbleUpon
  • Technorati
  • Google Bookmarks
  • Yahoo! Buzz

Category: Flash AS3

One Response to “Simple approaches to grabbing flashvars from HTML in Flash AS3”

  1. Brian

    I’ve been looking all over for a tutorial like this which includes a condition if the flashvar does not exist. Too bad it doesn’t show the html part.



Leave a Reply



Back to top