The typical (and fairly safe) way to call a JavaScript function from a SWF is
ExternalInterface.call.
However, Adobe AIR does
NOT support ExternalInterface, so we have to look for an alternative if we want to interact with
embeded SWFs
in HTML AIR apps. Before ExternalInterface was introduced to ActionScript, there was
fscommand, navigateToURL, and getURL:
<!-- This goes in your Adobe AIR application -->
<object id="swf" type="application/x-shockwave-flash">
<param name="allowScriptAccess" value="sameDomain">
<param name="movie" value="myFlashFile.swf">
</object>
<script type="text/javascript">
// this function must be in "window" scope
function myFunc(str) {
air.trace("Flash told me: " + str);
}
</script>
<!--
NOTE: myFlashFile.swf contains this AS2 code...
getURL('javascript:myFunc("some argument");void(0);');
-->
Just like typing javascript: addresses into your browser's address bar, that example allows
Flash to call a JavaScript
function. HOWEVER...
Most likely for security reasons, getURL requires user interaction. You can call
getURL in an on(release) handler, but if you just want the SWF to call a JavaScript
function at an arbitrary point in the timeline, there must be user interaction. The user must actually be clicking
or moving the mouse on top of the SWF at the exact moment getURL is called, or the
call will fail silently.
What does this have to do with cash prizes? Well, at work I'm writing a touchscreen AIR app that loads SWF presentations. The user is just supposed to watch the movie, but the movie needs to call a function on my HTML page from time to time. Obviously, I can't ask the user to keep tapping the screen or constantly move their finger around the screen during the presentation, but I haven't found a clean way to allow Flash to call my JavaScript function without user interaction. We figured out a cheesy workaround, but I'm certain there must be a "real" way to accomplish this. So here are the requirements:
This demo application reproduces the problem. Every four seconds, the
included SWF tries to call callMe(), a JavaScript function the app's HTML page. You can see that
while you move the mouse atop the SWF, the calls succeed, but as soon as you stop, so do the calls.
I will send (via PayPal or personal check, whichever you prefer) $50 of my own hard earned cash to the first person to meet ALL of the above requirements. You get the cash whether the solution is an easy one-liner or a crazy convoluted workaround. No haggling, please — we already figured out a hack for work, so this contest is only for my own satisfaction. And hopefully yours if you like money.
To submit your solution, please post a link in the comments on this page. You can save the solution as text on PasteBin if you don't have your own hosting.
To embed a link in your comment here, use BBCode-style tags. For example, [url]http://your.web.site/path/file.ext[/url]
I'm a Front-End Engineer at Yahoo! working on the Mail and Messenger teams. I blog about web design and development topics including accessibility, usability, performance, and developing HTML / CSS / JavaScript applications on Appcelerator Titanium and Adobe AIR.
If you're a web developer, you might enjoy Jelo, my JavaScript library.
A few panoramic shots I took at SDCC 2010. #geek http://bit.ly/bwX6GB
JS version of Regex prime number checker:
function isPrime(n) {
return Array(n + 1).join("1")
.search(/^1?$|^(11+?)\1+$/) == -1;
}
Погрузился в пучину EcmaScript5, местами увлекательно, местами нудно =)
Modernizr http://ow.ly/18njQ1
A Collection of 20 HTML5 Video Players - a round-up of JavaScript and html5 alternatives to Flash-based media player... http://ow.ly/18njQ2
jQuery TOOLS - The missing UI library for the Web http://ow.ly/18njQ3
Contactable - A jQuery Plugin | the odin http://ow.ly/18njQ4
Giants vs Dodgers, sweet seats. http://twitpic.com/2ag9pa
@snookca That'll be fixed next week. I promise.
@snookca I was tryna not name names ;) But really that was just par for the course today, pretty hectic day. As I'm sure you know.
Who breaks major stuff after 4pm on Friday? On the last day of the sprint, no less. Tsk. (wasn't me)
Awesome live git tracker for teams: http://www.utsup.com/
RT @DerrenBrown: Blog post: Camera Software Lets You See Into the Past http://bit.ly/9kjVg5
10 invites to the new version of Digg: http://bit.ly/dqM8EV
Threaded vs Evented Servers, great look at the whats and whys. http://bit.ly/bDUEjn #geek
Nav, Context menus, "app-style" toolbars in sample chapter http://bit.ly/csTRY8 of new YUI book http://bit.ly/cJINoV
Add a side-mounted End Call button to your iPhone 4: http://bit.ly/cGxPBD #funny #geekAll original work on this site is covered by a Creative Commons Attribution 3.0 license unless otherwise specified.
You may share or use any code or images from this site in any manner, for free, so long as reasonable effort has been made to give credit where due.
The views expressed in the posts and comments on this blog do not necessarily reflect the views of Yahoo!