Calendar

March 2007
M T W T F S S
« Feb   Apr »
 1234
567891011
12131415161718
19202122232425
262728293031  

Tag Cloud

Categories

Archives

Highest Rated

Most Viewed

Recent Posts

Recent Comments


« Apollo is here!! The Revolution has begun. | Main | Apollo Self Updating Applications »

Apollo ActionScript / JavaScript Bridge

By Rich Tretola | March 19, 2007Print This Post Print This Post
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...
1,685 views

Bridging between JavaScript and ActionScript is very easy with Apollo. The following example will show how to call JavaScript functions from ActionScript and vice-versa. Special thanks to Oliver Merk of New Toronto Group for his contributions towards this example.

The MXML file defines a handler function for the call from JavaScript and then initializes the function when the DOM_INITIALIZE Event is fired. It’s function simply launches an ActionScript Alert. The doHTMLAlert() function calls the JavaScript function that is located within the loaded html file by scoping it as follows:

  1. html.htmlControl.window.calledFromAS();

html is the id of the HTML control, htmlControl is the control automatically embedded within mx:HTML and window is the reference to the loaded content. calledFromAS() is defined in the htmlwithJS.html file shown below. Finally htmlwithJS.html also demonstrated a normal JavaScript alert called from itself.

MXML file:

  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <mx:ApolloApplication xmlns:mx=”http://www.adobe.com/2006/mxml”
  3. layout=”absolute”
  4. creationComplete=”init()”>
  5. <mx:Script>
  6. <![CDATA[
  7. private var calledFromJSHandlerFunction:Function = calledFromJSHandler;
  8. private function init():void{
  9. html.addEventListener(Event.DOM_INITIALIZE, domInitialized);
  10. html.location = “htmlwithJS.html”;
  11. }
  12. private function domInitialized(event:Event):void{
  13. html.htmlControl.window.calledFromJSHandlerFunction = calledFromJSHandlerFunction;
  14. }
  15. private function calledFromJSHandler():void {
  16. mx.controls.Alert.show(”ActionScript called from JavaScript”, “Alert”);
  17. }
  18. private function doHTMLAlert( ):void {
  19. html.htmlControl.window.calledFromAS();
  20. }
  21. ]]>
  22. </mx:Script>
  23. <mx:Button id=”alertBtn” label=”Call JavaScript from ActionScript”
  24. click=”doHTMLAlert()” x=”137″ y=”10″/>
  25. <mx:HTML id=”html” x=”137″ y=”40width=”339″/>
  26. <mx:Label x=”10″ y=”12text=”Normal MXML Button”/>
  27. <mx:Label x=”28″ y=”38text=”HTML component”/>
  28. </mx:ApolloApplication>

htmlwithJS.html

  1. <html>
  2. <script language=”Javascript”>
  3. function calledFromAS() {
  4. alert(’Hello from ActionScript’);
  5. }
  6. </script>
  7. <body>
  8. <input type=”button
  9. value=”Call ActionScript from JavaScript”
  10. onclick=”calledFromJSHandlerFunction()” />
  11. <br />
  12. <input type=”button
  13. value=”Normal JavaScriptAlert”
  14. onclick=”alert(’Hello from JavaScript’)”>
  15. </body>
  16. </html>

JavaScript alert called from ActionScript:

htmlandas2.jpg

ActionScript Alert called from JavaScript

htmlandas1.jpg

Share/Save/Bookmark

Topics: Adobe AIR, Tutorials |

6 Responses to “Apollo ActionScript / JavaScript Bridge”

  1. Martin Says:
    March 19th, 2007 at 8:03 pm

    Why do you need the calledFromJSHandlerFunction indirection, why can’t you call calledFromJSHandler directly?

  2. murat k.girgin » İlk Apollo Öğreticileri Says:
    March 20th, 2007 at 3:32 am

    [...] Apollo ActionScript / JavaScript Köprü uygulaması [...]

  3. Bryan Says:
    March 21st, 2007 at 2:50 am

    Rich,

    Have you created a localConnection between an Apollo based swf and a swf hosted in a browser? If so, how did you do it? Thanks-b

  4. Andrew Paul Simmons Says:
    May 30th, 2007 at 1:55 pm

    You know what we need…
    Video tutorials on how to make flex components. And more video tutorials on how to make them from flash components. Cuz, its kinda tricky at first. Anyone know of any?

  5. yonodo Says:
    August 10th, 2007 at 7:16 am

    Hi,I used your example but I just get an error in Flex Builder 2 ,and to be more specific,this is all I get:”Error #1006″:”calledFromAS” is not a function.

    Can you please answer me ,tx,

    yonodo

  6. yonodo Says:
    August 10th, 2007 at 11:46 am

    Ok,I’ve done it,it seamed that there were some problems in the html format

Comments