jQuery Plugin: externalInterface
Posted on Mar 20, 2010 @ 11:46 PM
A jQuery plugin for communication between ActionScript and Javascript . Licensed jointly under the GPL and MIT licenses.
This plugin has been tested with jQuery 1.3.2 and 1.4.2.
USAGE:
Requires jQuery to be loaded before the plugin. To use externalInterface, use jQuery to select the Flash movie by id, and then call the externalInterface method. The only required arguments are the method name you are calling in your ActionScript External Interface. Example: $('#myFlashMovie').externalInterface({method:'flashMethodToCall'});
You can also pass arguments to your Actionscript ExternalInterface method, a callback function for success, and a callback function for an error. Example: $('#myFlashMovie').externalInterface({method:'flashMethodToCall', args:'args for actionscript', success:function(data){ alert('success! flash returned: ' + data); },error:function(error){ alert('there was an error: ' + error); }});
DEMO:
Click here for a demo of externalInterface.
DOWNLOAD:
(function($)
{
$.fn.externalInterface = function(args)
{
this.each(function()
{
if(typeof(args.method) != 'undefined')
{
try
{
if(typeof(args.args) != 'undefined')
{
var data = this[args.method](args.args);
}
else
{
var data = this[args.method]();
}
if(typeof(args.success) != 'undefined')
{
args.success(data);
}
}
catch(error)
{
if(typeof(args.error) != 'undefined')
{
args.error(error);
}
}
}
});
return this;
};
})(jQuery);

29 Comments
@MyFlash
please see the demo. i'm alerting the results but you can do whatever you'd like with them.
how can i get the return value from my flash function into javascript? When i try
____
foo = $("#MyFlash").externalInterface({method:'moviePlaying'});
____
foo is the flash object.
When i try
__
$("#MyFlash").externalInterface({method:'moviePlaying', success:function(data){foo=data}});
or
foo = $("#MyFlash").externalInterface({method:'moviePlaying', success:function(data){return data}});
__
it doesnt work too.. damn it :(
I noticed that sometimes in heavy pages (with many javascript code) flash has not enough time to initialize external interface before invoking method. User gets error "error: TypeError: this[args.method] is not a function". Especially it noticeable when use swfobject.embedSWF(...).
My workaround is to check typeof (args.method) == 'undefined' and call setTimeout to delay invoke of the args.method. And finally, I added debugMode variable.
You can see my workaround here: http://техблог.рф/external-interface (site is in Russian language, but code is in English)
Hey,
This doesn't seem to work on OBJECT's that are in DIV's set to display:none. I'm trying to use this to set a new source of a flash player used in a modal pop up.
Otherwise works great! Thanks!
Thanks, runs perfect!
@Steven i found this on the web regarding IE and external interface. http://waww.com.au/ramblings/externalinterfaceaddcallback-object-doesnt-support-property-or-method
@dudemanthe187th thanks for this! i'll be testing and updating.
@jim it shouldn't matter, as long as your flash has an id attribute.
Hello,
Ive got this working with in browsers like chrome and firefox ect. but in IE it get an error saying 'object doesnt support property or method'.
any ideas on how to fix this?
Thanks
for those of you who want to pass multiple arguments, change line 13 to this
var data = this[args.method].apply(this, args.args);
then args can be an array of arguments.
how do you pass multiple args to actionscript?
Get get this work. args is undefined
So how do you get this to work with.using jquery flashembed instead of using swf Object?
the answer to my question might be fairly obvious now that i think about it - just use something like ExternalInterface.call("interfaceTest"). thanks!
nice plugin, but one aspect has me a little confused. it seems that the addCallBack in AS is "gated" by an onclick event in your html. this may have been done for demo purposes, but my question is can we remove the $('#flashMovie').externalInterface({... part from your click handler (function interfaceTest()) so that AS determines when and what it does with the args? i hope this makes sense. also, can you have a list of args (separated by commas)?
@fred - i think i made it in CS4, which is why it won't open in CS3.
@nuno - i'm not sure. i don't do any AS2 coding.
hi joseph,
it sounds like you have not declared your external interface in your flash movie.
- dave
thanks. It's works!!
Hi,
In plugin, an error is occurring in line "var data=this[args.method](args.args);" . The error is: "error:TypeError:Object doesn't support this property or method".
Anyone call tell me what is wrong.
Thank you
Hi.
This plugin works in Actionscript 2.0?
Thanks
Hi, that looks great but I can't open the example .fla file.
I'm using Flash CS3. Do you have an idea ?
Thanks.
thanks, you're welcome!
Screw the guys who said this plugin doesn't work, they don't know how to use it.
Thank you so much for a great and working plugin Dave!
you're welcome, glad you could use it!
hi dave,
it works perfectly, thanks so much.
check out the demo code, upload it to your server, and run it for yourself. the plugin works fine.
there's a lot of things you need to get right in order for javascript to communicate with actionscript via external interface.
This plugin DOES NOT work, don't waste your time
@mottramdo.com
are you getting actionscript errors in the demo code?
on: "error: TypeError: this[args.method] is not a function"
This is because you using your own code to embed .swf file. Use swfobject.js to embed .swf just like the author does, then this problem will gone.
However, after that I get this: Error in Actionscript. Use a try/catch block to find error.
do you have this online anywhere where i can see it?
have you defined your external interface in your flash movie?
Can't seem to get it to work. Keep getting this error:
error: TypeError: this[args.method] is not a function
Thanks