1

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.

3 Answers 3

2

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"); 
Sign up to request clarification or add additional context in comments.

1 Comment

I'm having trouble getting this to work in IE 8. Is this "good browsers only" or am I missing something? Works fine in Saf/Chrome/FF
1

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.

2 Comments

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.