8 Replies Latest reply on Sep 26, 2010 9:54 PM by Flex Rock

    Call Javascript From AIR - Flash

    Hatchling Studios

      I'm developing an AIR application in Flash that needs to call external javascript functions. Is it possible to call external js functions from AIR (Flash) or call js functions embedded in an html file? Any help or examples are appreciated.

        • 1. Re: Call Javascript From AIR - Flash
          Neobox75 Level 3

          Hi, what did you mean ? Call a JS function in an html which is in a  HTML component ?

          • 2. Re: Call Javascript From AIR - Flash
            Hatchling Studios Level 1

            I have an external .js file that contains functions that speaks to a third party application. What I would like to do is call to the functions in the .js file from my AIR app that is being developed in Flash.

             

            If that's not possible, is it possible to re-write the javascript functions in an html file, load the html file into my AIR app (Flash) and call to them that way?

            I'm able to load html files, I just can't seem to be able to call to the js functions. Again, this would be my second choice.

             

            Thanks for any help.

            • 3. Re: Call Javascript From AIR - Flash
              Neobox75 Level 3

              Ok, I dont think you can call a JS file directly but load a simple HTML file like this:

               

              <html>
              <head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                  <script language="javascript" type="text/javascript">
                      <!--
                          function addBlu(a, b) { return (a+b); }
                      -->
                  </script>
              </head>
              </html>

               

              And with my poor AS3 code:

               

              import flash.html.HTMLLoader;
              import flash.net.URLRequest;
              import flash.events.Event;

              var html:HTMLLoader = new HTMLLoader();
              html.load(new URLRequest("callJS.html"));
              html.width = 0;
              html.height = 0;
              html.addEventListener(Event.COMPLETE, onLoaded);

              addChild(html);

              function onLoaded(e:Event) :void
              {
                  trace("result (4+8) : "+e.target.window.addBlu(4, 8));
              }

               

              This will trace: result (4+8) : 12

               

              Hope this help.

              • 4. Re: Call Javascript From AIR - Flash
                springframework

                How would I call a javascript function thats not inside the html page.

                 

                I want to include JQuery (or other libraries) into an AIR project. I then want to call these JQuery functions on any webpage loaded into the HTMLLoader.

                • 5. Re: Call Javascript From AIR - Flash
                  Neobox75 Level 3

                  Hi, I dont know but you can create wrapers functions on ur html file that query your jquery functions if u dont find how to do that

                  • 6. Re: Call Javascript From AIR - Flash
                    mihnea1975 Adobe Employee

                    Hi,

                     

                    Assuming you would like to build a JavaScript project that needs to use JQuery (or other JS framework) how would you proceed? You would include a .js file in some HTML files in which you need such support. Using this approach, you can use js framework functionality from js and html.

                     

                    You can use this approach with AIR too (just like you are using AIRAliases.js or AIRIntrospector.js).

                     

                    If i did not understand your workflow correctly, please provide me with more details and i will see if i can help you.

                     

                    Regards,

                    Mihnea Ovidenie

                    AIR Engineering

                    • 7. Re: Call Javascript From AIR - Flash
                      springframework Level 1

                      I want to do the opposite of what you are describing.

                       

                      I want to include a .js file into an .as or .mxml file

                       

                      I then want to be able to execute that javascript file on any HTMLLoader that I have.

                       

                       

                      earlier it was shown that you can execute javascript from actionscript if the javascript is inside the html of the HTMLLoader like this:

                       

                      function onLoaded(e:Event) :void
                      {
                          trace("result (4+8) : "+e.target.window.addBlu(4, 8));
                      }

                       

                       

                       

                      but how would I call JQuery functions on the html inside the HTMLLoader if JQuery is not included in it.

                      • 8. Re: Call Javascript From AIR - Flash
                        Flex Rock Level 1

                        Hi,

                         

                            Instead of writting a jquery or javascript function inside a html page you can include your .js file in the index.template.html file which is generated by the air application <script language="javascript" src="your filename"></script>.

                         

                        Then u can call the javascript function from your .as or mxml file like this

                         

                        ExternalInterface.call(

                        functionname,arguments);

                        Thats it.

                         

                        Thanks & Regards,

                        Flex Rock.