title: How to invoke an ActionScript method from JavaScript (HTMLLoader) object in AIR?
body: <p>So I have an Application Sandbox HTMLLoader object which I create in <a href="http://en.wikipedia.org/wiki/Adobe_Integrated_Runtime" rel="nofollow noreferrer">AIR</a> and simply want to call <a href="http://en.wikipedia.org/wiki/ActionScript" rel="nofollow noreferrer">ActionScript</a> methods from <a href="http://en.wikipedia.org/wiki/JavaScript" rel="nofollow noreferrer">JavaScript</a>. In Flash, this is accomplished through our trusty <strong>ExternalInterface.addCallback()</strong> function. However in AIR, things are quite a bit different, and I just can't seem to get it to work.</p>
  
  <p>Here is a simplified overview of my project:</p>
  
  <p>My AIR (ActionScript) main:</p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p>And this is how I create my HTMLLoader object:</p>
  
  <pre><code>{
      _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);
  }
  </code></pre>
  
  <p>And at last, here is my snippet of JavaScript in my <strong>AirRoot.html</strong> file which is trying to call the public method <strong>as3Function()</strong> declared in my Main class:</p>
  
  <pre><code>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();
  }
  </code></pre>
  
  <p>What am I missing?</p>