Skip to main content
Known Participant
June 17, 2011
Answered

Passing in arguments from js --> as3 via ExternalInterface

  • June 17, 2011
  • 2 replies
  • 1917 views

Is it possible to use javascript to call a method in as3 and pass in an object argument? This would act as a setter in some sense...

Thanks in advance for any advice!

This topic has been closed for replies.
Correct answer Kenneth Kawamoto

You'd use ExternalInterface.addCallback to register a function call from JS:

// AS

// register call from Javascript
ExternalInterface.addCallback("callFromJS", callFromJS);

// display the message from Javascript
function callFromJS(s:String):void
{
    textfield.text = s;
}

Then call from JS:

// JS
flashObject.callFromJS("Hello from JS");

You may need to set allowScriptAccess to true in the HTML embed code.

2 replies

Kenneth Kawamoto
Community Expert
Kenneth KawamotoCommunity ExpertCorrect answer
Community Expert
June 17, 2011

You'd use ExternalInterface.addCallback to register a function call from JS:

// AS

// register call from Javascript
ExternalInterface.addCallback("callFromJS", callFromJS);

// display the message from Javascript
function callFromJS(s:String):void
{
    textfield.text = s;
}

Then call from JS:

// JS
flashObject.callFromJS("Hello from JS");

You may need to set allowScriptAccess to true in the HTML embed code.

kglad
Community Expert
Community Expert
June 17, 2011

oops, my error.  kenneth is correct, use addCallback().

but, you'll need to do alot more to define that flashObject.

Kenneth Kawamoto
Community Expert
Community Expert
June 17, 2011

Yes you need to target Flash object in JS. I use JQuery, which is super simple - I recommend it

kglad
Community Expert
Community Expert
June 17, 2011

sure.  just add the parameters after the function string:

ExternalInterface.call("functionname",param1,param2,...)