To embed a Flash file in an HTML page, I highly recommend downloading SWFObject if you don’t use it already. SWFObject is the best way I’ve found to embed flash content in an web page. It’s easy to use, standards-friendly, and makes defining parameters easier than ever. Plus, it lets you define alternate content if your user doesn’t have the Flash plugin which, with Apple’s grudge against Flash, is nice to have these days.
They key to passing a variable through your SWFObject code is going to be slightly different depending on what version of SWFObject you’re using. If you’re using SWFObject 1, the key is the parameter addVariable which is used as follows:
Simply add this line to your block of code like so:
SWFObject 2 instead uses a <param> tag named “flashvars” which takes the form:
<param name=”flashvars" value="variable_name=variable_value" />
If you’re not comfortable with object tags, you can use their online script generator and just copy/paste to your HTML. To tell the generator your variables, simply expand the box labeled “SWF definitions”, and at the bottom you will find a line called Flashvars where you can add your variable names and values. This will add the line:
<param name=”flashvars" value="myVar=Hello, World!" />
within your <object> tags. The final code with SWFObject 2 will look something like this:
Step 2: the ActionScript
If you’re using AS2, you can just go right ahead and use the variable in your flash file as you would any other variable, but AS3 requires an extra line of code:
var asVarName:VarType = this.loaderInfo.parameters.variable_name;
Effectively, you need to create a new variable in the ActionScript that is set to the variable you defined in the loaderInfo parameters. With our example above, the ActionScript would look like this:
var externalVar:String = this.loaderInfo.parameters.myVar;
Then you can use the variable externalVar, which now has the value “Hello, World!”, however you please, such as setting a text field value, changing a state, etc.