How to call an ActionScript method from a JavaScript object (HTMLLoader) in AIR?

So, I have an Application Sandbox HTML desktop object that I create in AIR and just want to call ActionScript methods from JavaScript . In Flash, this is achieved using our robust ExternalInterface.addCallback () function . However, in AIR, everything is completely different, and I just can not get it to work.

Here is a simplified overview of my project:

My AIR (ActionScript) main:

public class Main extends Sprite {

    public var _as3Var:String = "testing";
    public function as3Function():void
    {
        trace("as3Function called from Javascript");
    }

    public function Main() {
        NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, onInvoke);
    }

    protected function onInvoke(e:InvokeEvent):void {
        NativeApplication.nativeApplication.removeEventListener(InvokeEvent.INVOKE, onInvoke );
        var app = new App();
        addChild(app);
        app.init(new ExternalContainer(), e.currentDirectory, e.arguments);
    }
}

And this is how I create my HTMLLoader object:

{
    _html = new HTMLLoader();
    _html.useCache = false;
    _html.runtimeApplicationDomain = ApplicationDomain.currentDomain;
    _html.load(new URLRequest("sandbox/AirRoot.html"));
    _html.width = 800;
    _html.height = 600;
    App.ref.addChild(_html);
}

And finally, here is my JavaScript snippet in my AirRoot.html file that is trying to call the as3Function () public method declared in my main class:

Exposed.testAs3 = function()
{
    air.trace("Exposed.testAs3 called");            /* This works fine. */
    air.trace("runtimeVersion:");                   /* This works fine. */
    air.trace(air.NativeApplication.nativeApplication.runtimeVersion);  /* This works fine. */
    air.trace("seeing if I can get to AS3 params...");  /* This works fine. */

    /* This doesn't work - get the following error: TypeError: Value undefined does not allow function calls. */
    air.NativeApplication.nativeApplication.as3Function();
}

?

+3
2

, . , , , , , ...

, :

AIR HTMLLoader HTMLLoader.window, - JavaScript. , HTMLLoader.window = AS3Function; - ( , Main, Main):

_html.window.as3Function = Main.as3Function;

JavaScript as3Function :

<script>
    window.as3Function();
</script>

JavaScript "window.htmlLoader". - AS3 HTMLLoader, - _html. _html JavaScript.

+6

, AIR, javascript, :

<script>
  as3Function();
</script>
+1

All Articles