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);

18 Comments
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