« AIR HTML Control | Main | Simple Drag and Drop into AIR »
AIR ActionScript / JavaScript Bridge
| By Rich Tretola | February 25, 2008 | Print This Post
|
| 239 views |
Bridging between JavaScript and ActionScript is very easy with AIR. 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 when it was an Apollo sample.
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:
- html.htmlLoader.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:
- <?xml version="1.0" encoding="utf-8"?>
- <mx:WindowedApplication 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.HTML_DOM_INITIALIZE, domInitialized);
- html.location = "htmlwithJS.html";
- }
- private function domInitialized(event:Event):void{
- html.htmlLoader.window.calledFromJSHandlerFunction = calledFromJSHandlerFunction;
- }
- private function calledFromJSHandler():void {
- mx.controls.Alert.show("ActionScript called from JavaScript", "Alert");
- }
- private function doHTMLAlert( ):void {
- html.htmlLoader.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="40" width="339"/>
- <mx:Label x="10" y="12" text="Normal MXML Button"/>
- <mx:Label x="28" y="38" text="HTML component"/>
- </mx:WindowedApplication>
htmlwithJS.html
- <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:

ActionScript Alert called from JavaScript

Topics: Adobe AIR |







August 7th, 2008 at 1:43 am
nice one.. but can u plz help me in finding client ip address using flex3 and javascript????
very urgent………………
thanx in advance