views:

186

answers:

3

How could I set a Flash (Actionscript 3) variable using javascript?
Or is it possible to call a flash function with parameters from javascript?
I have tried document.getElementById('flash').SetVariable("data", "asdf");but it only works in AS2 and AS1.

A: 

SetVariable is no longer in use on AS3 because of the stricter sand-boxing, but it wasn't completely eliminated, you can still replace

SetVariable("varName","value")

By

FlashVars = "varName=value"

And access it via root.loaderInfo.parameters.varName.

However, I'd suggest using the new ExternalInterface class instead, read more about it here.

LiraNuna
Personally think you should elaborate more on `ExternalInterface` as it is the preferred method of interactions with Flash + JS.
Doug Neiner
@dcneiner: I'd love to, but I don't have much experience with it, so I linked to a good reference instead.
LiraNuna
+2  A: 

Like LiraNuna said, you should use ExternalInterface to communicate with flash. Here are the basics:

Step 1: Make a function in flash that sets the variable:

function setVar(value) {
    somevar = value;
}

Step 2: Use ExternalInterface to register the function:

var connection = ExternalInterface.addCallback("someFunctionName", null, setVar);

Step 3: Call your function from Javascript to set the variable:

var mySWF = document.getElementById("swfID");
mySWF.someFunctionName('some_value');

If you're using swfobject to embed your swf, another much easier option would be the addVariable method:

mySWF.addVariable("var_name", "value");
Goose Bumper
A: 

You could look into using faBridge. Details here: link text

Q-rius