Question

I am trying to call a actionscript method from javascript, but it says flashObj.method() is not a function. I have declared this method inside a submodule of my flash object. So how to call methods of sub modules

Was it helpful?

Solution

Here is a running sample with source code.

http://www.redcodelabs.com/2012/04/calling-actionscript-method-from-javascript/

You need to have a look into the ExternalInterface class. All you need is there. Also you need to be careful with error handling on both sides (AS3 or JS)

OTHER TIPS

Look at ExternalInterface.addCallback()

I'm fairly certain that this only works for methods that belong to the document root (or document class) and not methods of other classes.

As above answer states you need to use

ExternalInterface.addCallback("nameOfFunctionAsCalledFromJS",nameOfFunctionInFlash);

I recommend using different names for the 2 functions. You can use this to make publically available even functions of different classes, provided they are public. However, assuming that your modules are externally loaded swfs, when you load your swfs into the application you need to make use of LoaderContext and set the modules in the same ApplicationDomain and SecurityDomain as the app that loads them:

var lc:LoaderContext = new LoaderContext(true,ApplicationDomain.currentDomain,SecurityDomain.currentDomain);
loader.load(urlRequest,lc);

Additionally, you need to allow access from JS to your application (the one that loads the modules) and therefor you need to call the allowDomain before any calls from JS:

Security.allowDomain("yourdomain");
Security.allowInsecureDomain("yourdomai")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top