Javascript – Setting Flash (AS3) variable with Javascript


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.

Best Solution

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

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

mySWF.addVariable("var_name", "value");