Home Contact RSS

Actionscript Javascript Communication | ExternalInterface call and addCallback Tutorial

1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 3.67 out of 5)
Loading ... Loading ...

Overview:

Using ExternalInterface.addCallback() we can expose an actionscript function and make it available to javascript. This would be helpful to have your webpage with embedded flash communicate to your flash swf file and even control it with javascript! Say we wanted to have buttons in the html page that would control an object in the flash. Communication is a two-way road so wouldn’t it be great to be able to go the other way as well, you can! That is the main function of ExternalInterface! In this example/tutorial I will explain both directions of communication with flash and javascript! Communication between flash and javascript isn’t just a myth or mystery!

Steps:

  1. Be sure to import flash.external.*;
  2. Set up the javascript to actionscript lane of your communication road. (ExternalInterface.addCallback(methodName, instance, method);)
  3. Write your javascript function.
  4. Set up the actionscript to javascript lane. (ExternalInterface.call(functionNameInJavascript);)

We will follow the text’s journey on our road of communication…

The One way: I type in ‘Johnny Appleseed’ in the html text box and press the Send Text To Flash button. The onclick javascript event finds the flash element and calls it’s function (sendTextFromHtml) and then clears the text in the html box. This function has been set up and is exposed to javascript (in actionscript lines 4-7) with the methodName ’sendTextFromHtml’ while the method it calls is recieveTextFromHtml() in the actionscript. So ‘Johnny Appleseed’ is received as the parameter of the recieveTextFromHtml() function and is assigned to the text of the theText text box.

And back: Now I delete ‘Johnny Appleseed’ since he’s only a fable and enter ‘Paul Bunyan’ in the swf text box and press the Send From Flash to Javascript button. This calls the onRelease function associated with this button. ExternalInterface.call calls the ‘recieveTextFromFlash’ function in the javascript of the page and passes ‘Paul Bunyan’ as the parameter. The javascript function finds the html text box using getElementById() and assigns the parameter to the value of that text box!

This technique will even work if you’re not sending folklore character down the road.

Example:

View the live example here: ActionscriptJavascriptCommunication.html

Actionscript Javascript Communication thumbnail
Actionscript:

  1. import flash.external.*;
  2.  
  3. //Set up Javascript to Actioscript
  4. var methodName:String = "sendTextFromHtml";
  5. var instance:Object = null;
  6. var method:Function = recieveTextFromHtml;
  7. var wasSuccessful:Boolean = ExternalInterface.addCallback(methodName, instance, method);
  8.  
  9. //Actionscript to Javascript
  10. //ExternalInterface.call("recieveTextFromFlash", _root.theText.text);
  11.  
  12. function recieveTextFromHtml(t) {
  13. _root.theText.text = t;
  14. }
  15.  
  16. _root.button.onRelease = function() {
  17. ExternalInterface.call("recieveTextFromFlash", _root.theText.text);
  18. _root.theText.text = "";
  19. }

Javascript:

  1. function recieveTextFromFlash(Txt) {
  2. document.getElementById('htmlText').value = Txt;
  3. }

HTML: view Source of sample page

Download:

Download all source files (.fla, .html, .swf): ActionscriptJavascriptCommunication.zip

Tags: , , , , , , , , ,

Related posts

flashden banner

Gravatar

i-e said,

February 14, 2008 @ 10:58 am

hi,

great sollution, but in does it also works in IE ?

Gravatar

Evan Mullins said,

February 15, 2008 @ 5:06 pm

Good Question, half of this works in IE…
You can communicate from flash to javascript just fine. But getting from the javascript to the flash is giving me trouble. I get the “Object doesn’t support this property of method” error.

Any insight would be helpful!

Gravatar

Guro Khundadze said,

February 24, 2008 @ 4:50 pm

i have the same problem in IE :(
“Object doesn’t support this property of method”

Gravatar

neerJ said,

March 20, 2008 @ 2:21 am

hav a flv fullscreen player,inside a html page,

in that html page one button is there,

that button should be invisible until

user click & watch half of the flv movie,

after watching that half of the movie,
that html button should be automatically active,

any idea how to do this type of thing

using javascript in flash

plz mAIL ME AT neeraj8585@gmail.com

Gravatar

Evan Mullins said,

March 20, 2008 @ 12:01 pm

If I understand your question and situation correctly, You have a button in your html that you want to only be visible after a user has watched a certain amount of the movie?

I’d put a function in your player that would check to see the amount of video the user has watched, and then, once it reached 50% (or whatever percent you need) have it call a jsfunction with ExternalInterface.call(). You’d have to have this javascript function in your html page and have it hide/show the specific button… (help with that here: Javascript Code to show a hidden element)
Let me know if that helps!

Gravatar

neeraJ said,

March 24, 2008 @ 1:37 am

hii Evan Mullins

thanks for ur reply,

i am able to trace the flv cue points,but getiing confusion in calling that function,can u help me to calling that function, just tell how do i call that function from flash to hide that html button in start & when it will reach to that particular point that html button will automatically shown, it will be very helpful for me.

////////////////////////////////////////////////
import flash.external.*;

// for calling javascript external class

// plaese writr that function here that all

/**
Requires:
- FLVPlayback component on the Stage with an instance name of my_FLVPlybk
- TextArea component on the Stage with an instance name of my_ta
*/

import mx.video.*;
my_FLVPlybk.contentPath = “http://www.helpexamples.com/flash/video/water.flv”;
my_ta.visible = false;
// create cue point object
var cuePt:Object = new Object();//create cue point object
cuePt.time = 1.444;
cuePt.name = “elapsed_time”;
cuePt.type = “actionscript”;
my_FLVPlybk.addASCuePoint(cuePt);//add AS cue point
// add 2nd AS cue point using time and name parameters
my_FLVPlybk.addASCuePoint(5.3, “elapsed_time2″);
var listenerObject:Object = new Object();
listenerObject.cuePoint = function(eventObject:Object):Void {
my_ta.text = “Cue at: ” + eventObject.info.time + ” occurred” ;
my_ta.visible = true;
};
my_FLVPlybk.addEventListener(”cuePoint”, listenerObject);
///////////// script end ///////////////////////

regards,
Neeraj

Gravatar

Evan Mullins said,

March 24, 2008 @ 9:48 am

//actionscript function this goes in your flash
function call jsfunction() {
ExternalInterface.call(”showButton”);
}

//javascript function (this needs to be on the html page.
//you’ll need to give the button you want to show an id of myButton
//or change this to whatever you have… and set it’s style=”display: none;”
function showButton() {
document.getElementById(”myButton”).style.display = “”;
}

Gravatar

saga said,

March 26, 2008 @ 12:37 am

hi

The sample is nice and I changed the code to work in IE also ……the code

change the html code as follows………..

in javascript change like this

if (navigator.appName.indexOf(”Microsoft”) != -1)
{
document.getElementById(’IEflash’).sendTextFromHtml(document.getElementById(’htmlText’).value);
} else
{ document.getElementById(’flash’).sendTextFromHtml(document.getElementById(’htmlText’).value);
}
document.getElementById(”htmlText”).value = “”;

I hope this may help for some one………

Gravatar

Michael said,

March 31, 2008 @ 11:01 am

Looks great! How would you go about sending data from multiple text boxes?

Gravatar

Evan Mullins said,

March 31, 2008 @ 4:27 pm

With ExternalInterface.call, the first argument is the name of the js function you want to call in your HTML document, and the rest are arguments you want to pass in to it. You can send many, just separate them with a comma.
so: ExternalInterface.call(”recieveTextFromFlash”, _root.theText.text);
becomes: ExternalInterface.call(”recieveTextFromFlash”, _root.theText2.text, _root.theText.text, _root.theText3.text);
Check the documentation of ExternalInterface.call from Adobe
call (ExternalInterface.call method)
public static call(methodName:String, [parameter1:Object]) : Object
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.

Gravatar

nmduc073 said,

April 4, 2008 @ 4:27 am

Hi saga,
Please post your full html code. Where is IEflash?

Gravatar

Michael said,

April 7, 2008 @ 12:30 pm

I’ve got the html/javascript submitting multiple inputs but it’s the actionscript side that I am completely clueless about.

Gravatar

ashok said,

April 24, 2008 @ 3:37 am

hi,thanks for your hlep

Gravatar

raghavender said,

April 24, 2008 @ 3:39 am

thanks,this help me a lot

Gravatar

Jonah said,

June 3, 2008 @ 7:31 pm

Quite clear and thorough instructions. Thanks for writing this.

Gravatar

Auz said,

July 30, 2008 @ 6:04 am

Hi saga …

Can you explain where the IEFlash object is coming from on your fix for IE …

is the object embedded in a different way to flash ???

Thanks …

Gravatar

polluxx42 said,

August 26, 2008 @ 1:07 pm

No such luck, original example does not work in IE7.

RSS feed for comments on this post · TrackBack URI

Leave a Comment