2 Replies Latest reply on Oct 29, 2010 3:25 PM by Stenrap2

    Call javascript from AIR

    Stenrap2 Level 1

      Hi all,

       

      I'm having a hell of a time trying to call JavaScript from an AIR application. Here's what I'm trying to do:

       

      1. Load an HTML page using HTMLLoader.

      2. Access a link by its ID in the HTMLLoader object and simulate the JavaScript click() function on that link.

       

      I've tried lots of approaches, but here's the latest code that fails:

        • 1. Re: Call javascript from AIR
          Stenrap2 Level 1

          Accidently submitted my post with some keyboard combination.

           

          Here's the JavaScript function in the loaded HTML page (it simply submits a form on the page):

           

          function submitAssistantForm(direction)

          {

              document.theForm.direction.value=direction;
              document.theForm.submit();
              return;
          }

           

          And here's the ActionScript code I'm trying:

           

          var html:HTMLLoader = new HTMLLoader();

          html.addEventListener(Event.COMPLETE, splash);

          html.load(new URLRequest("...URL omitted..."));

           

          private function splash(e:Event):void

          {

              // try to access the element by its ID and call the Javascript click() function on it

              html.window.document.getElementById('HrNext').click(); // this gives error "click is not a function"

           

              // try to call the Javascript function directly by its name

              html.window.submitAssistantForm('next'); // this gives error "TypeError: Result of expression 'document.theForm' [undefined] is not an object."
          }

           

          By the way, if I add the HTMLLoader object to the stage and manually click the link with my mouse everything works perfectly (so I have to believe it's possible to programmatically send a click event to the link). I feel like I'm so close. Can anyone point me in the right direction?

           

          Thanks,

          Rob

          • 2. Re: Call javascript from AIR
            Stenrap2 Level 1

            In case this helps anyone else, I figured out one way to do it. Just  set the value of window.location to the href value of the link:

             

            <code>html.window.document.location =  "javascript:submitAssistantForm('next');";</code>