Calendar

March 2007
S M T W T F S
« Feb   Apr »
 123
45678910
11121314151617
18192021222324
25262728293031

Tag Cloud

Categories

Archives

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

htmlwithJS.html

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

JavaScript alert called from ActionScript:

htmlandas2.jpg

ActionScript Alert called from JavaScript

htmlandas1.jpg

Topics: Adobe AIR, Tutorials | 16 Comments »

16 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?

    Reply to this comment

  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

    Reply to this comment

  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?

    Reply to this comment

  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

    Reply to this comment

  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

    Reply to this comment

  7. Apollo ActionScript / JavaScript Bridge | EverythingFlex: Flex & AIR « action script Says:
    April 27th, 2010 at 8:53 pm

    [...] Więcej: Apollo ActionScript / JavaScript Bridge | EverythingFlex: Flex & AIR [...]

  8. nike shoes sale Says:
    May 4th, 2010 at 4:21 am

    When the lamp is off,you will find the shadow everywhere. Friend is who can give youstrength http://www.nikejordanshoes2sell.com/ at last.

    Reply to this comment

  9. mayfaire ugg Says:
    May 4th, 2010 at 4:27 am

    Gratitude ed hardy clothing is the sign of noble souls.

    Reply to this comment

  10. nike shox Says:
    June 10th, 2010 at 3:08 am

    In New York, China’ U.N. wholesale nike shox Ambassador Li Baodong a href=”http://www.adikshoes.com/ “>wholesale nike air max said after Wednesday’ vote that the sanction were aim at curb nonprolifer and would not affect “the normal life of the Iranian people” nor deter normal trade activity.Iranian Presid Mahmoud Ahmadinejad wa set to arriv in China later Thursdai to tour the World Expo in Shanghai, but he wa not expect to hold talk with senior Chines leaders.

    Reply to this comment

  11. nike shox R4 Says:
    June 10th, 2010 at 3:09 am

    In New York, China’ U.N. wholesale nike shox Ambassador Li Baodong wholesale nike air max said after Wednesday’ vote that the sanction were aim at curb nonprolifer and would not affect “the normal life of the Iranian people” nor deter normal trade activity.Iranian Presid Mahmoud Ahmadinejad wa set to arriv in China later Thursdai to tour the World Expo in Shangha.

    Reply to this comment

  12. laptop batteries Says:
    June 21st, 2010 at 3:02 am

    very good code, I had downland this.

    Reply to this comment

  13. evden eve nakliyat Says:
    June 25th, 2010 at 4:09 pm

    thanks for blog

    Reply to this comment

  14. vibram five fingers Says:
    July 13th, 2010 at 5:03 am

    vote that the sanction were aim at curb nonprolifer and would not affect “the normal life of the Iranian people” nor deter normal trade activity.Iranian Presid
    http://www.vibram-fivefingers.info cheap vibram five fingers
    http://www.vibram-fivefingers.info barefoot shoes
    http://www.vibram-fivefingers.info/vibram-five-fingers-classic-c-4.html Vibram Five Fingers Classic
    http://www.vibram-fivefingers.info/vibram-five-fingers-flow-c-5.html Vibram Five Fingers Flow

    Reply to this comment

  15. supra shoes Says:
    August 7th, 2010 at 5:57 am

    http://suprashoes365.com

    Reply to this comment

  16. cheap vibram fivefingers Says:
    August 17th, 2010 at 3:53 am

    We have been in business for over 8 years on various of cheap vibram fivefingers ; we have over 1000 items of stock and the experience to match. you can go through http://www.walkbarefooted.com to get more info.Friendly, efficient, and knowledgeable staff. We have a real passion for the goods we sell! If you would like to add a testimonial of your own, please email us a testimonial to sales@walkbarefooted.com, we are always interested in your comments.

    Reply to this comment

Comments