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>
2 "src", "${swf}",
3 "width", "${width}",
4 "height", "${height}",
5 ... more ...
2 body { background-color: pink; }
3 </style>
2 document.TestJS.sendMessage('you rock')
3 }
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:
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]