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:

Here it is in action

It appears as though you don't have the required Flash player or you have JavaScript turned off.

Here's the full code from the above file.

Download the sample files (.zip)

Post any questions in the comments!