Passing variable from JavaScript To AS3 (Flash CS3)

By | July 4, 2007

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);

0 Kudos

12 thoughts on “Passing variable from JavaScript To AS3 (Flash CS3)

  1. Sergey Kovalyov

    Does it really differ comparing to AS2? I just pass all the variables I need using well known FlashVars and access them in any Class via Application.application.parameters associative array (e. g. Application.application.parameters.userId).

  2. Pingback: myFeedz - Articles on "cs3"

  3. Pingback: - Flash

  4. Pingback: AS3 Tuturial - Variables from JavaScript To Flash CS3 « Flash Enabled - Get Ready With Flash…

  5. Pingback: Beedigital, talking about Flash & Resources

  6. Pingback: passare dei parametri alla Document Class (FCS3) - Wave9 Forums

  7. Pingback: Metah - A Web Technologist Adventure » Blog Archive » Holy Grail: SEO Model for Flash and Flex Content

  8. Pingback: flashcrobat » Blog Archive » Flash and JavaScript

  9. Pingback: Flash tutorials | Flash vs Javascript |

  10. Kenneth Paulino

    Thanks for the trace function. I’ll snippet it for later use. How can you send a var from AS3 to Javascript? Or modify a Javascript var from within Flash. I want to grab the width and height from the onmetadata of a FLV, resize the player according to those values, and then send out those values to be used with swfobject. I have all the vars placed and AS3 complete, I just need to know how to send them out so swfobject can read them. Thanks!!

  11. Pingback: Easy GameJolt Flash Api integration for Games (As3) - Monster Brain inc