Metah - A Web Technologist Adventure

Tweets

Passing variable from JavaScript To AS3 (Flash CS3)

July 4th, 2007 at 4:06 pm by Ahmet Gyger

Posted in ActionScript3, Flash, RIA, Tutorials, Web Design



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

Related posts:

  1. Using URLVariables()
  2. Holy Grail: SEO Model for Flash and Flex Content
  3. JSON and AS3 URLRequest Class

11 Responses to “Passing variable from JavaScript To AS3 (Flash CS3)”

  1. Sergey Kovalyov
    July 4th, 2007 17:52
    1

    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. myFeedz - Articles on "cs3"
    July 4th, 2007 22:33
    2

    [...] Passing variable from JavaScript To AS3 (Flash CS3) details » http://www.metah.ch Posted 6 hours ago by Metah Flash Blog feed details » [...]

  3. feed-squirrel.com - Flash
    July 5th, 2007 02:29
    3

    [...] Passing variable from JavaScript To AS3 (Flash CS3) 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 proj [...]

  4. AS3 Tuturial - Variables from JavaScript To Flash CS3 « Flash Enabled - Get Ready With Flash…
    July 5th, 2007 10:55
    4

    [...] You can find the tutorial here. [...]

  5. Beedigital, talking about Flash & Resources
    July 5th, 2007 13:29
    5

    [...] View tutorial here. [...]

  6. passare dei parametri alla Document Class (FCS3) - Wave9 Forums
    September 5th, 2007 13:20
    6

    [...] ciao, in realta’ questo e’ un falso problema perche’ questi parametri eventualmente GIA’ esistono in loaderInfo.parameters: http://www.metah.ch/blog/2007/07/04/…as3-flash-cs3/ a occhio e croce direi che a livello di document class gli unici parametri che puoi passare in fase di creazione sono quelli via FlashVars. Jaco a scanso di equivoci, prima di interpretare una risposta leggi con cura qui: http://www.linuxtrent.it/documentazi…deIntelligenti wannabeFlasher.addEventListener(event.WORLD, learn); [...]

  7. Manuel Fritsch
    November 16th, 2007 15:18
    7

    Man, you saved my day, thanks a lot!

  8. Metah - A Web Technologist Adventure » Blog Archive » Holy Grail: SEO Model for Flash and Flex Content
    April 2nd, 2008 17:39
    8

    [...] To access the XML URL from ActionScript: [...]

  9. flashcrobat » Blog Archive » Flash and JavaScript
    April 21st, 2008 18:35
    9

    [...] A brief and basic recollection about passing vars from JS to AS3. found on: metah.ch [...]

  10. Flash tutorials | Flash vs Javascript | Lemlinh.com
    August 28th, 2008 21:55
    10

    [...] Read more [...]

  11. Kenneth Paulino
    January 15th, 2009 23:50
    11

    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!!

Leave a Reply