Reputation: 6797
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.
Upvotes: 1
Views: 5395
Reputation: 15872
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");
Upvotes: 2
Reputation: 67232
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.
Upvotes: 1