View comments | RSS feed

addCallback (ExternalInterface.addCallback method)

public static addCallback(methodName:String, instance:Object, method:Function) : Boolean

Registers an ActionScript method as callable from the container. After a successful invocation of addCallBack(), the registered function in Flash Player can be called by JavaScript or ActiveX code in the container.

Availability: ActionScript 1.0; Flash Player 8

Parameters

methodName:String - The name by which the ActionScript function can be called from JavaScript. This name does not need to match the actual name of the ActionScript method.

instance:Object - The object to which this resolves in the method. This object is not necessarily the object on which the method can be found -- you can specify any object (or null).

method:Function - The ActionScript method to be called from JavaScript.

Returns

Boolean - If the call succeeded, returns true. If it failed because the instance was not available, a security restriction was encountered, there was no such function object, a recursion occurred, or something similar, returns false.

A return value of false may also mean that the containing environment belongs to a security sandbox to which the calling code does not have access. You can work around this problem by setting an appropriate value for the allowScriptAccessOBJECT tag or EMBED tag in the HTML of the containing environment.

Example

The following example registers the goToMacromedia() function as callable from the container with the name goHome.

import flash.external.*;

var methodName:String = "goHome";
var instance:Object = null;
var method:Function = goToMacromedia;
var wasSuccessful:Boolean = ExternalInterface.addCallback(methodName, instance, method);

var txtField:TextField = this.createTextField("txtField", this.getNextHighestDepth(), 0, 0, 200, 50);
txtField.border = true;
txtField.text = wasSuccessful.toString();

function goToMacromedia() {
    txtField.text = "http://www.macromedia.com";
    getURL("http://www.macromedia.com", "_self");
}

For the previous example to work properly, you should copy and paste the following code into the containing HTML page. This code relies on the id attribute of the OBJECT tag and the name attribute of the EMBED tag to have the value externalInterfaceExample. The function thisMovie returns the appropriate syntax depending on the browser, since Internet Explorer and Netscape refer to the movie object differently. Unless the HTML page is hosted on a server, your browser may alert you with a security warning.

Note: Avoid using other methods of accessing the plug-in object, such as document.getElementById("pluginName") or document.all.pluginName, because these other methods do not work consistently across all browsers.

<form>
    <input type="button" onclick="callExternalInterface()" value="Call ExternalInterface" />
</form>
<script>
function callExternalInterface() {
    thisMovie("externalInterfaceExample").goHome();
}

function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName]
    }
    else {
        return document[movieName]
    }
}
</script>

Version 8

Comments


Beaver82 said on Sep 24, 2005 at 9:16 AM :
seems not to work the javascript>flash call
No screen name said on Sep 29, 2005 at 7:04 AM :
JavaScript to Flash 8 communication does not work correctly in "local trusted" mode, as it should (but there is no problem when doing the other way around: Flash 8 can communicate wtih JavaScript with no problem in "local trusted" mode).

I have read this page by Deneb Meketa:
http://www.macromedia.com/devnet/flash/articles/fplayer8_security_print.html

That says "trusted" SWF files are allowed to communicate with JS, and JS with trusted SWF in both ways, when in "Local Trusted" mode. Well, this doesn't seem to work for me. I've put a text file inside the appropriate "FlashPlayerTrust" folder, as told by that article above, but after doing that it only allows SWF-->JS communication (ExternalInterface.call), but not JS-->SWF communication (ExternalInterface.addCallback).

I've tried displaying the system variable "System.security.sandboxType" into my Flash movie and it says "localTrusted", so I've done correctly the steps required to get the SWF file to the "local trusted" status, but still I can only communicate from SWF to JS, but not the other way around (JS to SWF).

If I publish the SWF online in a website, then it works both ways correctly, but I need my SWF files to run locally.

I hope this gets fixed.

OMA
quamei said on Oct 11, 2005 at 12:43 PM :
looks like when a callback is made, the callback function cannot take a parameter for the external browser like the call function. Is there a why to get around that . Call an actionscript funtion that take parameters from the browser with javascript passind data to the actionscript funtion.
shimi2 said on Oct 13, 2005 at 12:45 PM :
Hi OMA - You need to allow the SWF access to the HTML containing your Javascript.
The DevNet security article you read talks about 2 ways to do this:

1) It sounds like you've configured the FlashPlayerTrust trust file so Flash Player will trust the directory that contains your SWF. You also need to trust the directory that contains the HTML file that contains the Javascript function. Add another line to that trust file with the appropriate directory.

2) Change the value of the allowScriptAccess html object/embed tag attribute. Please read the section "allowScriptAccess Default" in the DevNet Security article for more info. (It's in the latter half of the article.)

Hope that helps.
No screen name said on Nov 8, 2005 at 7:37 AM :
Thanks for your comments, shimi2

My SWF, HTML and JS files are in the same directory, so with one line in "FlashPlayerTrust" pointing to that directory should be enough. About the second point, I have allowScriptAccess with an "always" value and it still doesn't work (also tried with "sameDomain"). I don't know what to do to get JS->SWF working (it works the other way around).

Thanks for your replies.
OTGI said on Dec 28, 2005 at 12:02 PM :
ExternalInterface.addCallback does not register methods when the swf is run from a local drive (i.e. c:\ or network drive) regardless of the sandbox settings (e.g. Local Trusted). I have set my global security settings to 'Always Allow' and allow all domains. I can use getURL without any problems and any swf-html calls without a problem. However, html-swf communication via the use of ExternalInterface.addCallback does not work. THIS IS A BUG.
No screen name said on Feb 22, 2006 at 9:14 AM :
It took me many hours of banging my head against the wall to figure this one out....

Do not declare your JavaScript functions, etc, that are to be used by ExternalInterface in the <head> section of the html page. You will get very erratic behavior and the inability to pass variables back and forth. Keep it in the <script> tag AFTER the Flash <object>, <embed> tags are declared....

After that was cleared up, I am successfully passing JSON objects back and forth and slowly regaining sanity.

So much for following 'standard practices.'
ActionScriptGuru said on Feb 23, 2006 at 11:13 PM :
FYI!!! The example above has an error. The value of instance CANNOT be
null, it should be this. This holds true on the Mac platform not sure about
Windows, but definetely on Mac.
No screen name said on Mar 23, 2006 at 5:16 AM :
I can't pass an Array (filled with various objects) from Javascript to Actionscript. Firefox 1.5 is generating an error: "Error calling method on NPObject" and IE 6 hangs.

When I try passing a String it works fine.

I think it's a bug since I can get a very complex object from AS to JS using the ExternalInteface.call method, but not the another way around (with a registered callback function).
BillBrown said on Mar 24, 2006 at 12:28 PM :
For those of you having troubles implementing ExternalInterface.addCallback on Firefox, but have it working on Internet Explorer - the name attribute in the <object> tag seems to give Firefox problems. If you remove the name attribute, it appears to work properly (you can still leave in the id attribute).

The ExtInt.html sample file that ships with Flash at
C:\Program Files\Macromedia\Flash 8\Samples and Tutorials\Samples\ActionScriptxternalAPI
does not use the name attribute in the <object> tag - yet the HTML code generated by Dreamweaver 8 does include the name attribute when you embed a Flash movie and then name it through the Property Inspector.

I'm guessing that Firefox is having issues with this because it more closely follows web standards for valid markup.
Dauntless said on Mar 25, 2006 at 3:18 PM :
@FlashGuru: Yeah, I noticed that too. (And why should it be different on PC ?)
student student said on Jun 26, 2006 at 12:25 PM :
I found that in MSIE a call from JavaScript to Flash does not work when the
SWF is nested with in the FORM tag. IE says the SWF id name is
undefined. You move the SWF outside of the form and it work great.

Make sure your SWF is out side of your Form tag. Do you know a work
around????
Ɓukasz Grela said on Sep 11, 2006 at 4:17 PM :
Hi, I had some problems with communication JS -> FLASH on Firefox which appeared as "Error calling method on NPObject!" i tried to call a function in body section through onLoad event

function:

function documentLocation()
{

var location = document.location.href;
if(document.all) {
document.all.flashName.SetVariable("documentLocation", location);
} else {
document.flashName.SetVariable("documentLocation", location);
}
}

in body i called simply:

<body onLoad="documentLocation()" >

then nothing happened and in javascritp console in firefox i've noticed mentioned error, i did some search and i found interesting solution:

var timer = 0;
function documentLocation(location)
{
timer = setTimeout("setMyVariable()",500);
}
function setMyVariable()
{
clearTimeout(timer);
var location = document.location.href;
if(document.all) {
document.all.flashName.SetVariable("documentLocation", location);
} else {
document.flashName.SetVariable("documentLocation", location);
}
}

adding slight delay solved my problem, hope that it helps You as well
No screen name said on Sep 19, 2006 at 6:16 PM :
A bug is found in FireFox 2.0 Beta, Player 9.0, when using ExternalInterval calling a flash function from javascript. Flash player crashes with no warning.
Sergey Sov said on Sep 21, 2006 at 1:56 AM :
I try to use ExternalInterface in my Flash module. But IE browser (ver 6, WIN XP) generates javascript error when I put my OBJECT tag inside the FORM tag on the HTML page. This is an error with description: "'MyFlashModule' is undefuned". I can escape from this error by this method below:
If I remove the "id" attribute from the OBJECT tag and put the "name" attribute instead of "id" then another error will be appeared. Please see description below:
Line: 56
Char: 3
Error: Syntax error

Could you fix this problem, please?
steve mckenzie said on Nov 26, 2006 at 7:15 PM :
working with this is sure not easy.

after executing 1 function from my swf, its like the entire dom on the page gets destroyed and no more JS functions will execute properly. The one to the swf will, but everything else just dies!

I have an integration with jQuery as my JavaScript library. I tried all these tricks people suggested and it still wont work.

It would be great if someone can contact me and help me out. I'm trying to make a media player that flash is only the controller and JS/ html / CSS is the UI.

thanks.
micheSWE said on Dec 12, 2006 at 5:02 AM :
I get it to work in Mozilla but not in Internet explorer. What could be the problem?
NEON24 said on Jan 11, 2007 at 9:11 PM :
function CreateMovForm(sName) {
var oMov;
if (navigator.appName.indexOf ("Microsoft") !=-1) {
top.sLEAD_Browser = "MS";
oMov = window.document.getElementById(sName);
} else {
top.sLEAD_Browser = "FF";
oMov = document[sName];
}
return oMov;
}
SBROrange said on Jan 19, 2007 at 6:04 AM :
Problem with ExternalInterface when using Firefox, I have a page where I have used a flash swf to interact with the server since due to a cross-domain problem.
Anyway the first thing I do is to send flash a variable from javascript with that variable I go to the server using Flash Remoting and once the response is received I send the data to a javascript function to do this:

document.getElementById(item).innerHTML = arg;

If I do that the browser closes down with no alert, error or anything.
The information is received because I made an alert(arg) and the function receives the data.
Can anyone help me on this?

Thanks a lot.
lillybw said on Mar 14, 2007 at 1:46 PM :
The problem with the form tag is addressed in this article:
http://www.adobe.com/go/kb400730
No screen name said on Apr 17, 2007 at 1:38 PM :
i hope this can help people.

If you are calling a javascript function from flash and the javascript function calls another function that returns a value, the other functions return will be intercepted by flash and the initial javascript function you called from flash will NOT finish running.

Ran into this problem when trying to have one flash send another flash a message to stop playing audio. I was using the examples getFlashMovie function for testing ie..i solved it by not calling that function and simply doing an if test within the function that flash called. execution was then allowed to continue and the other flash was told to stop playing.
Roknine said on Apr 29, 2007 at 6:59 PM :
I am having a few problems here with this. I have made a simple button
bar with the following code:

import flash.external.ExternalInterface;
//this function was tested and works perfectly
function hidecurrent(hideit:MovieClip):Void {
hideit.enabled = false;
hideit._alpha = 40;
}
ExternalInterface.addCallback("hcurrent", this, hidecurrent);
stop();
//this next function repeats 6 times for different buttons
this.air_mc.onRelease = function() {
getURL("lgair.html");
}

What I am trying to do is call the hidecurrent function from a html
document: This is what I have so far. Inside the head tag I have:

<script src="Scripts/AC_RunActiveContent.js" type="text/javascript"></
script>
<script language="JavaScript">
var flashObject;

function initialize() {
if(navigator.appName.indexOf("Microsoft") != -1) {
flashObject = document.greenbar;
flashObject.hcurrent(air_mc);
}
else {
alert("At least this is loading");
flashObject = window.document.greenbar;
flashObject.hcurrent(air_mc);
}
}
</script>

And then I instantiate the flash movie inside the body tag with:

<div style="margin-top:15px; margin-bottom:12px;"><center><script
type="text/javascript">
AC_FL_RunContent( 'type','application/x-shockwave-
flash','data','greenbar.swf','width','720','height','68','movie','greenbar' ); //
end AC code
</script><noscript><object type="application/x-shockwave-flash"
data="greenbar.swf" width="720" height="68" >
<param name="movie" value="greenbar.swf" />
<param name="allowScriptAccess" value="always" />
<embed src="greenbar.swf" quality="high" width="720" height="68"
name="greenbar" align="middle" allowScriptAccess="always"
type="application/x-shockwave-flash" pluginspage="http://
www.macromedia.com/go/getflashplayer" />
</object></noscript></center></div>

I think my problem is that the flashObject is not correctly linking to
greenbar. However, according to the book I am reading, this should work
perfectly. Is this some bug or and I missing something obvious.
z1pdaneo said on Jun 15, 2007 at 5:59 AM :
Is it possible to register multiple functions by using addCallBack?
For example,
ExternalInterface.addCallback("f1", this, f1);
ExternalInterface.addCallback("f2", this, f2);
I'm having trouble to excute the 2nd funciton from javascript.
jsw_nz said on Jul 2, 2007 at 3:57 PM :
There has been a lot of discussion on this topic: JavaScript to Flash communication in Firefox using Adobe's AC_RunActiveContent.js.

After a long period of goggling the topic, came across this thread - which explains the node list parameter that gets returned. So the standard function needs a slight modification:

function thisMovie(movieName) {
if (navigator.appName.indexOf("Microsoft") != -1) {
return window[movieName];
}else{
if(document[movieName].length != undefined){
return document[movieName][1];
}
return document[movieName];
}
}

// see http://www.ozzu.com/ftopic74795.html fo FF bugfix

Hope this helps someone trying to get it to work inside FF
cheers
No screen name said on Jul 11, 2007 at 8:55 AM :
I just spent few hours to find a solution to make a call from my javascript to my actionscript callback function...and it appears that the problem came from the use of jquery syntax to get a reference to my Flashmovie:

I was using $("#myFlashMovieId") and I got error when trying to call the actionscript function.
Using the classic javascript syntax:
var flashmovie = document.getElementById("myFlashMovieId");
flashmovie.myActionscriptMethod();
Fix my problem!

Hope it helps.
No screen name said on Oct 23, 2008 at 4:52 PM :
I had the problem where calling a function(in Flash Active Script) from
JavaScript was giving a "Type Error" with message stating "is not a valid
function".

On the URL of the browser, I type in:
javascript:try { document.getElementById("FLVPlayer").onNextVideo(22); }
catch (e) {alert(e); }

It gives me an type error "onNextVideo is not a function". This works fine
on IE and Chrome though - no errors - it changes the video successfully.

Finally I figured out the issue. I had to do it this way:

function getFlashMovieObject()
{ var movieName = "FLVPlayer";
if(document.embeds[movieName])
return document.embeds[movieName];
if(window.document[movieName])
return window.document[movieName];
if(window[movieName])
return window[movieName];
if(document[movieName])
return document[movieName];
return null;
}

function onPlayVideo(value)
{ var oFlv = getFlashMovieObject();
oFlv.onPlayVideo(value); }

Now everything works on every browser I tested. FINALLY I'm HAPPY

Sekhar.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/8/main/00002201.html