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 doing the following:

Example

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

import flash.external.*;

var methodName:String = "goHome";
var instance:Object = null;
var method:Function = goToAdobe;
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 goToAdobe() {
    txtField.text = "http://www.adobe.com";
    getURL("http://www.adobe.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>

See also

allowDomain (security.allowDomain method)


Flash CS3


Comments


Chads20000 said on Aug 8, 2007 at 1:17 PM :
This was racking my brain for ever.

If you have the calling function / method as

myFunction = funciton() {}

the add callback will not initialize and fail everytime. The calling function / method must be

function myFunction(){}

----------------------- Correct ------------------------
import flash.external.*;
var wasSuccessful:Boolean = ExternalInterface.addCallback("MyJavaFunction", null, myFunciton);

function myFunction(){
... your code here
}

 

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

Current page: http://livedocs.adobe.com/flash/9.0/main/00001653.html