Passing variable from JavaScript To AS3 (Flash CS3)

A piece of Cake
Passing vars from JS to AS3 is really easy, but it’s not as easy as it was with AS2.
Here I use the excellent SWFObjects (I use it in all my web projects now) to pass variables to the Flash Player:

so.addVariable("var1", "value1");
so.addVariable("var2", "value2");

Now to get the values with AS3 when you already know the variable name:

var myValue1:String = this.loaderInfo.parameters.var1;
var myValue2:String = this.loaderInfo.parameters.var2;

If you want to trace all the variables (names and values) passed to the Flash Player:

for ( var theName:String in this.loaderInfo.parameters ) {
var theValue:String = this.loaderInfo.parameters [theName];
trace(“from JS :”+ theName+” “+theValue);
}

11 Comments

Post a Comment

Your email is never shared. Required fields are marked *