View comments | RSS feed

call (ExternalInterface.call method)

public static call(methodName:String, [parameter1:Object]) : Object

Calls a function exposed by the Flash Player container, passing 0 or more arguments. If the desired function is not available, the call returns null; otherwise it returns the value provided by the function. Recursion is not permitted; a recursive call produces a null response.

If the container is an HTML page, this method invokes a JavaScript function in a <script> element.

If the container is some other ActiveX container, this method broadcasts an event with the specified name, and the container processes the event.

If the container is hosting the Netscape plug-in, you can either write custom support for the new NPRuntime interface or embed an HTML control and embed Flash Player within the HTML control. If you embed an HTML control, you can communicate with Flash Player through a JavaScript interface that talks to the native container application.

Availability: ActionScript 1.0; Flash Player 8

Parameters

methodName:String - The name of the function to call in the container. If the function accepts parameters, they must appear following the methodName parameter.

parameter1:Object [optional] - Any parameters to be passed to the function. You can specify zero or more parameters, separating them by commas. The parameters can be of any ActionScript data type. When the call is to a JavaScript function, the ActionScript types are automatically marshalled into JavaScript types. When the call is to some other ActiveX container, the parameters are encoded in the request message.

Returns

Object - The response received from the container. If the call failed (for example if there is no such function in the container, or the interface was not available, or a recursion occurred, or there was a security issue) null is returned.

Example

The following example calls the JavaScript function sayHello() in the HTML page that contains the SWF. The call is made by using the ExternalInterface.call() method.

import flash.external.*;

var greeting:String;
var btn:MovieClip = createButton(100, 30, 0xCCCCCC);
btn.onPress = function() {
    greeting = String(ExternalInterface.call("sayHello", "browser"));
    this.mcTxt.text = greeting; // >> Hi Flash.
}

function createButton(width:Number, height:Number, color:Number):MovieClip {
    var depth:Number = this.getNextHighestDepth();
    var mc:MovieClip = this.createEmptyMovieClip("mc_" + depth, depth);
    var mcFmt:TextFormat;

    mc.beginFill(color);
    mc.lineTo(0, height);
    mc.lineTo(width, height);
    mc.lineTo(width, 0);
    mc.lineTo(0, 0);

    mcFmt = new TextFormat();
    mcFmt.align = "center";
    mcFmt.bold = true;

    mc.createTextField("mcTxt", depth, 0, 0, width, height);
    mc.mcTxt.text = "Call JS Function";
    mc.mcTxt.setTextFormat(mcFmt);

    return mc;
}

For the previous example to work properly, you should be copy and paste the following code into the containing HTML page. Unless the HTML page is hosted on a server, your browser may alert you with a security warning.

<script>
    function sayHello(name) {
        alert(">> Hello " + name + ".");
        return ">> Hi Flash.";
    }
</script>

Version 8

Comments


nuthing.\ said on Dec 12, 2005 at 11:40 AM :
Dont forget to make sure allowScriptAccess="always" in the HTML object/embed code
No screen name said on Dec 16, 2005 at 7:32 AM :
Hi, i get a bug when using externalInterface.call method on internet explorer behind some proxy, not all. the method return null. I think of a security issue but which one. is there a way to resolve this and does the network security can have an impact ?

Thanks
No screen name said on Jan 17, 2006 at 8:11 AM :
I have the same problem when using the given example but placing it inside a html form. At that momen Internet explorer gives an error: 'testjava' is undefined (testjava is the name of my movie)
when i remove the class id from the object description i don't get the error but it still returns 'null'. This is only on Internet Explorer.
No screen name said on Feb 14, 2006 at 1:47 PM :
ExternalInterface does not work for all ASP.NET page (aspx) because the flash control must be placed inside a FORM tag.

When the flash control is inside a form tag an ID attribute can not be given to the control as this causes a javascript error.

When will this bug be fixed?

Has anybody managed to get ExternalInterface working on an ASP.NET webpage?
doken said on Feb 15, 2006 at 5:37 PM :
Yes, same problem as above, returning null on IE and working in FireFox and Netscape. Have set...
<PARAM NAME="AllowScriptAccess" VALUE="always">
...but it did not resolve this.
Can we have some sort of a response from the developers please?
kossok said on Mar 9, 2006 at 4:37 AM :
Hi, Did you make sure you set that in the OBJECT and the EMBED tags?

This works for me in IE and FF

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>external_int</title>
</head>
<body bgcolor="#ffffff">

<script>
function sayHello(name) {
alert(">> Hello " + name + ".");
return ">> Hi Flash.";
}
</script>

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="550" height="400" id="external_int" align="middle">
<param name="allowScriptAccess" value="always" />
<param name="movie" value="external_int.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src="external_int.swf" quality="high" bgcolor="#ffffff" width="550" height="400" name="external_int" align="middle" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
</body>
</html>
No screen name said on Mar 12, 2006 at 8:45 PM :
I was going to use it to control position/size of some elements inside my Flash app depending on the width and height of a browser window. So I declared a couple of javascript functions like these:
function getWidth(){
var avWidth = document.getElementById('body').clientWidth;
return avWidth;
}
function getHeight(){
var avHeight = document.getElementById('body').clientHeight;
return avHeight;
}
I use this Action to control the position of a movie clip:
import flash.external.*;
ball.onEnterFrame = function() {
this._x = ExternalInterface.call("getWidth", "browser")-82;
this._y = ExternalInterface.call("getHeight", "browser")-82;
};
Actually it works fine on my local web server. The only bad thing is that my Mozilla after a time says the script makes Mozilla to run slowly and asks if I'd like to abort it or not. So far I did not notice it in IE.
I think it's because of "onEnterFrame". Can somebody advise what is the best way to control position or dimensions of some movie clip/clips please.
MarkDog12 said on Mar 21, 2006 at 11:16 AM :
How would you call a C++ function? I have my flash movie embedded as an ActiveX control. Can ExternalInterface be used to call C++ functions? Can it be used to register ActionScript functions within your C++ container?
Charles Parcell said on Mar 28, 2006 at 10:52 AM :
Make sure yo have both

<param name="allowScriptAccess" value="always" /> // object tag
allowScriptAccess="always" // embed tag

_and_

id="somename" // object tag
name="somename" // embed tag
No screen name said on Mar 30, 2006 at 8:52 AM :
1) calling a window.close() Javascript function via the new ExternalInterface functionality caused IE and Firefox to crash. Fixed this by calling window.close using setTimeout

2) in IE, the Flash needs to be given an ID attribute in the HTML when you want to use ExternalInterface, else the browser throws an error when trying to talk back to Flash
daRoost said on May 4, 2006 at 11:40 AM :
for those wondering how to call setTimout with window.close here is the line of code : ExternalInterface.call("window.setTimeout","window.close()",50);
gareth_cole said on May 18, 2006 at 9:20 AM :
Can you give us more info on what can and can't be passed as function arguments please?

For example, I've found out the hard way that passing empty strings gives the value 'null', and passing strings containing carriage returns chr(10) bombs completely.

It would be useful to have a list of what data and datatypes can't be passed via the external interface
NotFFirk said on Jul 20, 2006 at 9:33 AM :
for IE and <form>
try to do smth:
<script>
var movie1;
myfunc(...);
</script>
<form
<object id="movie1" bla bla bla.../object>
</form>
<script>
movie1 = document.getElementById('movie1');
</script>
AlexWS said on Aug 3, 2006 at 2:56 PM :
As koosok said his code works fine on both IE and FF.

You also need to check if the <object> tag is inside a form, if it is, you need to put the swf outside the form.

Also I'm able to get complex objects and long strings
Lacrymocéphale said on Aug 17, 2006 at 9:41 AM :
A strange and blocking thing :
Under Internet Explorer, the add of the well known VBScript for version plug-in detection designed by MM cause a ExternalInterface.call bug "uncorrect character".

<script language="VBScript" type="text/vbscript">
<!-- // Programme d'assistance Visual Basic requis pour détecter les informations de version du contrôle ActiveX Flash Player
Function VBGetSwfVer(i)
on error resume next
Dim swControl, swVersion
swVersion = 0

set swControl = CreateObject("ShockwaveFlash.ShockwaveFlash." + CStr(i))
if (IsObject(swControl)) then
swVersion = swControl.GetVariable("$version")
end if
VBGetSwfVer = swVersion
End Function
// -->
</script>
cybercow said on Sep 13, 2006 at 6:35 AM :
This simple "Hallo World" example worked fine and i wanted to integrate it into real use, then it taked me about 3 hours ! to figure out why my real use example not works ! First i tihinked that is about 'allowScriptAccess' or <object> 'id' or 'name' but wasn`t, then i started to think that is maybee browser malfunction (tryed in IE and FF) then i wanted to leave it... because thinked that i`m crazy -> the example works, then switched to my new "real example" not.... the code is the same ! hey !

But the difference was: the example has only 1 frames, but my project has 3 frames (1 and 2) for standard and basic preloader .... and 3 is working frame.... then i followed the instructions and puted the import sentence
import flash.external.*; into the first frame --- WRONG !!! this must go onto the frame where you realy execute ExternalInterface calls the third in my example, because else - ExternalInterface will not work and you will be wery wery nervous.....

also i noticieted that coding import flash.external.*; or import flash.external.ExternalInterface; is not the same - but not shure about this, the first works for me.

I hope that this will help someone...
No screen name said on Oct 15, 2006 at 12:32 PM :
If you have weird problems and using ExternalInterface - this may help ...

So you can have JS call Flash and vice versa. Sounds simple hey? But think about this .... take JS calling Flash ... the JS call is akin to an interrupt. Flash being an independent running process, which has all sorts of execution paths going on, suddenly gets called by JS. Does Flash stop what it's doing and jump execution to fullfill the JS call? Does it queue the JS function call, finish what it's doing, and then get to it?

Ultimately - are there concurrency issues here to think about? In my experience - yes. The exact same on IE and Firefox browsers too.

I ran into all sorts of bizarre symptoms - like data corruption, bizarre execution flow etc. JS is an event driven environment, with events firing at any time. Someone, somewhere, has to control the flow between these environments - and it seems some of that job is left to us.

Unfortunately, Flash has no features to really help with concurrency (like semaphors). And no documentation I could see addressed this issue. So I was left at trial and error.

Anyway, this is how I fixed the problems :

I use messages to pass "commands" back and forth between Flash and JS, which makes this easier to do. For JS -> Flash communication, the Flash routine that gets called simply queues the message (in an array). On an OnEnterFrame() execution path, the message is taken off the queue and is processed. Thus, I never have processing occurring directly from the JS call, so the processing of two calls around the same time cannot step on each other. It is possible the queue could become corrupt, but I never experienced this.

In processing the message, I found that it was fine for Flash to then call a JS function directly. But I had problems if code that handled a button click event (for example), directly called a JS routine. I do not understand why, but controlling outgoing calls with the same onEnterFrame() method fixed the problems. So an outgoing message is queued and an onEnterFrame thread actually calls JS with this msg.

Basically, this all boils down to - if you have JS calling Flash, or Flash calling JS ... do the actual work on an onEnterFrame entry point. I have not tried, but it would probably work just as well using a setInterval() entry point.

You may need to do likewise in the JS environment. In my case, I do likewise on the other end, but this is another plug-in (JS is just a middle-man passing the messages between these plug-ins).
jhannus said on Nov 9, 2006 at 10:58 AM :
yes, i would also like to know if there is a length limit on String parameters passed to ExternalInterface.call(). if so what is the limit???
No screen name said on Nov 15, 2006 at 3:17 AM :
HI.
I tried to use the above methods to call a java function with the help of embedded flash file. but nothing works for me. i am using MX 2004 to make files. the publish settings does not show flash player 8 option its only up to version 7. i am using IE5 . i have tried similar example in vb.net desktop application , which works fine with callback. but i cant open fla file which comes with that example. that example passes arguments in xml format. can anybody tell me whats going on please !!

Thanks.
ScottyDawg said on Nov 22, 2006 at 1:05 AM :
Here's a random problem to look out for...it's not a bug, per se, but I pulled my hair out trying to figure out what was happening:

I was making a call using ExternalInterface.call() to a method with an alert() command in it, which is fine, normally. BUT the alert() was attempting to trace a misspelled parameter:

function myFunc ( myID ) {
alert( myId ); // "d" should be uppercase
// do other stuff
}

This won't necessarily throw a JS error in-browser, but it WILL prevent the ExternalInterface.call() from succeeding. Change "myId" to "myID" and all's well.
grecu said on Jan 10, 2007 at 3:43 AM :
javascript integration kit can be used for flash to js communication

http://weblogs.macromedia.com/flashjavascript/
M. Bilal Arshad said on Jan 15, 2007 at 2:50 AM :
The Function is not running .. i have done as described in the sample program . even that program is not running . tell me what to do its urgent . Its always returning null no matter what i do . I have lowered the browser setting to lowest and done that enable thing in the settings and also i have set the allowActionScript = "always" . What else should i do .
No screen name said on Jan 16, 2007 at 6:42 AM :
To make ExternalInterface work in IE/ASP.NET webpage; declare a variable in javascript with the same name as the id of the flash object and reference it to the object:

<object id="flashId" classid="...
</object>
<script>
var flashid = document.getElementById("flashId");
</script>

Now, Flash can find the Flashobject in IE DOM when you use the ExternalInterface.

ankepulco
Lacrymocéphale said on Apr 11, 2007 at 5:37 AM :
More about <script> tags :
A simple flash file with only the following action
import flash.external.*;
ExternalInterface.call("alert", "Test");

If in the <head> of the embeding page, the first <script> tag is a VBScript one, you'll get with IE the "uncorrect character" error.

<script language="VBScript" type="text/vbscript"></script><script language="javascript" type="text/javascript"></script>
and you got the bug.

<script language="javascript" type="text/javascript"></script><script language="VBScript" type="text/vbscript"></script>
and you won't get the bug.

PS: don't forget the <param name="allowScriptAccess" value="always" /> for local tests.
No screen name said on May 16, 2007 at 1:09 AM :
OK good
ExternalInterface work in IE/ASP.NET webpage
IE 7, VISTA, Visual web 2005
thanks "ankepulco"
---------------------------aspx page-------
........
<body>
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="550" height="400" id="test2" align="middle">
<param name="allowScriptAccess" value="always" />
<param name="movie" value="test-2.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src="test-2.swf" quality="high" bgcolor="#ffffff" width="550" height="400" name="test-2" align="middle" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
<script>
var test2 = document.getElementById("test2");
</script>

<form id="form1" ........................
---------------------------END aspx page-------
---------------------------Action S-------
import flash.external.*;

var greeting:String;
var btn:MovieClip = createButton(300, 30, 0xCCCCCC);
btn.onPress = function() {
greeting = String(ExternalInterface.call("sayHello"));
this.mcTxt.text = greeting; // >> Hi Flash.
}
function createButton(width:Number, height:Number, color:Number):MovieClip {
var depth:Number = this.getNextHighestDepth();
var mc:MovieClip = this.createEmptyMovieClip("mc_" + depth, depth);
var mcFmt:TextFormat;

mc.beginFill(color);
mc.lineTo(0, height);
mc.lineTo(width, height);
mc.lineTo(width, 0);
mc.lineTo(0, 0);

mcFmt = new TextFormat();
mcFmt.align = "center";
mcFmt.bold = true;

mc.createTextField("mcTxt", depth, 0, 0, width, height);
mc.mcTxt.text = "Call JS Function";
mc.mcTxt.setTextFormat(mcFmt);

return mc;
}
No screen name said on Jun 1, 2007 at 9:24 AM :
Tip: I discovered that if you have a javascript function return either an empty-string, or a string of spaces, the resulting retun value from the call to ExternalInterface.call("thatFunction") will be the string "null". I submitted this as a bug report.
No screen name said on Jun 12, 2007 at 2:54 AM :
It does not works at the browser, Oprea. (I'd tested from Mac.) Is anyone knows this problem? How could I resolve this problem? Please add a new comment if somebody kwnos solution for this problem.

Thanks.
xuzhao said on Jun 13, 2007 at 10:15 PM :
I just saw this interesting issue and also blocked by similar problem in my development. Fortunately I saw discussions here and also a very useful technote from Adobe website. I hope these could help others who'll have such kind of problems. The Adobe TechNote link is below: http://kb.adobe.com/selfservice/viewContent.do?externalId=kb400730&sliceId=2

 

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/00002203.html