A few days ago I did some mentoring with a ColdFusion developer in Baton Rouge. He was also doing some Flex and I got to take a look at some of his work. He was making use of a feature I had not seen before, ExternalInterface. This is a rather interesting little feature. It lets you create a bridge between your Flex code and JavaScript code on the page. This is not the same as the Flex Ajax Bridge (which apparently was a Labs product and then moved into core Flex 3). I plan on looking at that more later, but here is some basic info about ExternalInterface.
At a simple level, ExternalInterface allows for two way communication between anything in the browser that uses JavaScript (remember that other plugins may have a JavaScript API, and other Flex/Flash embeds may have exposure as well). So I could write JavaScript code that runs some stuff in my Flex app. I could write Flex code that will fire off something in JavaScript.
Here is a quick example of listening for events in Flex.
2 <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init()" width="220" height="200">
3
4 <mx:Script>
5 <![CDATA[
6
7 public function init():void {
8
9 ExternalInterface.addCallback('sendMessage',getMessage)
10
11 }
12
13 private function getMessage(m:String) {
14 thebox.text+=m
15 }
16
17 ]]>
18 </mx:Script>
19
20 <mx:TextArea id="thebox" />
21
22 </mx:Application>
The ExternalInterface.addCallback API allows you to specify a function name to listen for (sendMessage) and a corresponding function to run in your Flex code. In this case, I've built an API to let me pass strings to a TextArea. Very exciting, I know.
For testing, I modified the file, index.template.html. If you are new to Flex, you may not know this, but Flex uses a template HTML file to generate your view when you run your application. You don't have to use this HTML in production. You can just use the SWF. For my testing I modified this so it would be easier to work with. This template is a bit large (I'll include the source in a zip at the end) so I'll just paste in the modifications I made.
Normally your Flex application will take over the entire page. I wanted a mix of HTML and Flex. If you look at your template, you will notice the embed code uses tokens for height and width:
2 "src", "${swf}",
3 "width", "${width}",
4 "height", "${height}",
5 ... more ...
Now, if you are like me, you may wonder - where in the heck do those values come from? I had a hard time finding this in the docs. I did find an excellent blog post on it though: Macros that are available in html-template files . Long story short - if I add a width and height to my MXML Application tag (as you see in the first code listing), it will get respected in the HTML template. To be sure it was working, I also added this lovely design:
2 body { background-color: pink; }
3 </style>
Pink is the new black. You heard it here first folks. Ok, so I added a simple button:
Add added the following JavaScript:
2 document.TestJS.sendMessage('you rock')
3 }
The basic idea for 'speaking' to the Flex application is to use the name of embed (also a token, it comes from the Flex project name) and call the method you exposed.
Here is a quick screen shot:

Ok, not so terribly exciting, but you get the idea. Note that document.X is Netscape only, it would be window.X in IE.
The flip side to this is calling JavaScript from Flex. This is done with the call API:
As you can see, you just name the function and pass the arguments.
I'm not 100% sure where I'd use this just yet - and I want to dig more into the Flex-Ajax bridge, but for a deeper look, please see this excellent blog entry:
Comment 1 written by todd sharp on 9 July 2009, at 12:34 PM
It's also common to use EI with 'invisible' Flash/Flex SWF uploaders - the SWF handles the upload and exposes the internal functions to JS.
One more thing - the YouTube chromeless player uses ExternalInterface to let you play/pause etc via JavaScript.
So bottom line, cool stuff and tons of use cases...
Comment 2 written by Dan Vega on 9 July 2009, at 12:40 PM
Comment 3 written by Gareth Arch on 9 July 2009, at 12:47 PM
Comment 4 written by Gareth Arch on 9 July 2009, at 12:49 PM
http://dougmccune.com/blog/2007/05/15/multi-line-s...
Allows you to inject a js file into the page and call methods in it. This way you don't have to hard code anything in the actual HTML wrapper, but rather just put it in your JS library.
Comment 5 written by Raymond Camden on 9 July 2009, at 1:25 PM
Comment 6 written by Campbell Anderson on 9 July 2009, at 3:25 PM
http://blog.xsive.co.nz/archives/268
Comment 7 written by TJ Downes on 9 July 2009, at 4:06 PM
Comment 8 written by jason olmsted on 10 July 2009, at 9:34 AM
One of the niftier aspects of communicating with external javascript is that you can resize and move your flash content. In a simpler instance, with wmode=transparent, you can create drop down menus with flash.
[Add Comment] [Subscribe to Comments]