Actionscript Javascript Communication | ExternalInterface call and addCallback Tutorial

Google Buzz

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

NEW live example with swfobject2 works in IE! ActionscriptJavascriptCommunication2.html

Actionscript Javascript Communication thumbnail
Actionscript:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import flash.external.*;

//Set up Javascript to Actioscript
var methodName:String = "sendTextFromHtml";
var instance:Object = null;
var method:Function = recieveTextFromHtml;
var wasSuccessful:Boolean = ExternalInterface.addCallback(methodName, instance, method);

//Actionscript to Javascript
//ExternalInterface.call("recieveTextFromFlash", _root.theText.text);

function recieveTextFromHtml(t) {
_root.theText.text = t;
}

_root.button.onRelease = function() {
ExternalInterface.call("recieveTextFromFlash", _root.theText.text);
_root.theText.text = "";
}

Javascript:

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

HTML: view Source of sample page

Download:

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

  • del.icio.us
  • Digg
  • email
  • Facebook
  • FriendFeed
  • Google Bookmarks
  • Hexosearch
  • LinkedIn
  • Mixx
  • Print
  • PDF
  • StumbleUpon
  • Technorati
  • Twitter
  • RSS
This entry was posted in tutorial and tagged , , , , , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

35 Comments

  1. i-e
    Posted 14 February 2008 at 10:58 am | Permalink

    hi,

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

  2. Posted 15 February 2008 at 5:06 pm | Permalink

    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!

  3. Guro Khundadze
    Posted 24 February 2008 at 4:50 pm | Permalink

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

  4. neerJ
    Posted 20 March 2008 at 2:21 am | Permalink

    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

  5. Posted 20 March 2008 at 12:01 pm | Permalink

    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!

  6. neeraJ
    Posted 24 March 2008 at 1:37 am | Permalink

    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

  7. Posted 24 March 2008 at 9:48 am | Permalink

    //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 = “”;
    }

  8. saga
    Posted 26 March 2008 at 12:37 am | Permalink

    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………

  9. Posted 31 March 2008 at 11:01 am | Permalink

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

  10. Posted 31 March 2008 at 4:27 pm | Permalink

    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.

  11. nmduc073
    Posted 4 April 2008 at 4:27 am | Permalink

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

  12. Michael
    Posted 7 April 2008 at 12:30 pm | Permalink

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

  13. ashok
    Posted 24 April 2008 at 3:37 am | Permalink

    hi,thanks for your hlep

  14. raghavender
    Posted 24 April 2008 at 3:39 am | Permalink

    thanks,this help me a lot

  15. Jonah
    Posted 3 June 2008 at 7:31 pm | Permalink

    Quite clear and thorough instructions. Thanks for writing this.

  16. Auz
    Posted 30 July 2008 at 6:04 am | Permalink

    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 …

  17. polluxx42
    Posted 26 August 2008 at 1:07 pm | Permalink

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

  18. Roland
    Posted 10 September 2008 at 4:09 am | Permalink

    Can anybody help me. I need to Get value from flash to js by calling js method.

  19. oxyde
    Posted 10 September 2008 at 4:14 am | Permalink

    great stuff. I think this might be the solution I looking for.
    I have a website in flash and html. The homepage is a flash image gallery with large picture preview related to a vertical scrolling thumbnails and a small menu at the bottom.
    When you click on one of the item of the menu you are send on a html page with a css replica of the flash picture gallery.
    I would like to click on a thumb located on the html page and be send back to the homepage and have the homepage gallery remember which image i’ve just clicked and display that one in my large image box.

    how do i tell the flash xml image gallery which one was clicked on the html page?

    cheers,
    oxyde

  20. Posted 10 September 2008 at 9:25 am | Permalink

    Thanks for the comments everyone, I took a minute recently and worked with this IE issue. I tried saga’s suggestion, but it didn’t seem complete. Then I decided to update my embed codes, and I used my favorite embed option swfobject2! Doing this alone fixed the IE bug! Now we have actionscript javascript communication in any browser, IE included.
    NEW live example with swfobject2 works in IE! ActionscriptJavascriptCommunication2.html

    @Roland – to do that, if I understand your question correctly, you’d have to just expose an actionscript function to javascript with addCallback and have it return the value you need and call that from javascript like any other function. Good luck!

  21. Posted 17 September 2008 at 12:28 pm | Permalink

    Object doesn’t support this property or method.

    I am able to communicate from Flash to JavaScript, NO PROBLEM. However, the inverse is not true. From JavaScript to Flash, I get the above error. There is no explanation for this all code is correct. All are set, the tag is there also.

    Can anyone help?

  22. YVR
    Posted 22 September 2008 at 6:47 pm | Permalink

    Evan – You said you fixed the codecs for that IE problem….did oyu update the zip file with that change ?

  23. Posted 22 September 2008 at 9:55 pm | Permalink

    @YVR – I fixed the embed codes for IE so the javascript can now “find” the flash element on the page correctly. Nothing in the flash source changed- only the HTML. I didn’t include that in the zip file, but it is in the html page here ActionscriptJavascriptCommunication2.html, just view source. My apologies for the confusion

  24. YVR
    Posted 22 September 2008 at 11:08 pm | Permalink

    Wonderful…Thank you Evan for your quick reply…this helps me a lot..let me try tonight..

  25. YVR
    Posted 23 September 2008 at 12:39 am | Permalink

    Hi Evan….This works perfectly as expected…but need a help on one step further down.
    I just added another function in the javascript function which looks like this (hope this comments will allow me to paste JS syntax…)

    function recieveTextFromFlash(Txt) {
    document.getElementById(‘htmlText’).value = Txt;
    DisplaySettingsPage();
    }

    In my Javascript page..
    DisplaySettingsPage {
    ajaxpage(‘products.asp’ ,’centerarea’)
    }

    Now when I click the Flash button it perfectly display the products.asp (which just does a select recordset from a mysql table) in the correct “centerarea” div..
    Now my question is..how do I modify the code so that the resultset is not displayed in DIV but rather in a nice formated textbox in FLASH (instead of centerarea DIV) ?
    -Let me know if I have confused you.
    -V

  26. Posted 24 September 2008 at 10:57 am | Permalink

    Do you know if this also works with AC_RunActiveContent.js. I tried using swfobject and it blows my design apart.

    thanks

  27. Posted 24 September 2008 at 12:40 pm | Permalink

    @Eric – I would think so, as long as IE can still address the flash content with the getElementById(). Let us know if it does work!

    @YVR – I admit, I’m confused. Not sure what you mean. You want the function coming from flash to send text back to flash? That just seems redundant…

  28. motu
    Posted 4 October 2008 at 1:34 am | Permalink

    Hi, is there a fairly easy way to get the text sent from flash
    to simply send the text as a javascript call to the web page?

    I’m fairly green & it would help with my learning.
    Thanks
    motu

  29. Posted 23 October 2008 at 5:07 pm | Permalink

    hi
    first sorry for my bad language
    i am hamada frpm jordan i tried to write a combination with javascript and flash 8
    but it doesn’t work only if it was published to web server
    why i can’t do it at home ???
    please reply

  30. Posted 6 November 2008 at 2:14 am | Permalink

    Is it as simple to set a cue point in a video and pass a ? variable?

    I have built many flash players, but honestly I like Jeoring’s player but I would like to load different content into my side column from a database like ads that are relational to the content of the video.

    I am currently looking into AMPHP but saw this post. Very informative stuff.

    Can you shoot me an email if you respond please?

  31. Dan
    Posted 15 May 2009 at 5:57 am | Permalink

    Evan,

    Your most recent update to ActionscriptJavascriptCommunication2.html does not work in ie6. I’m still getting “null is null or not an object”. More obviously, the flash part of the page is not loading. Could you clarify this issue?

    Dan

  32. Bob
    Posted 20 May 2009 at 5:39 am | Permalink

    Hi all,
    Very helpful site – thanks for all the effort. I have found an issue with one of your links. For the new swfObject2 example you are pointing to the incorrect location. It should be ‘/wp-content/plugins/wp-swfobject/2.0/swfobject.js’ – you have missed out the ‘2.0/’ from the url. This will fix the non-displaying example.

    Thanks,

    Bob

  33. Posted 21 May 2009 at 11:56 am | Permalink

    @Dan – Thanks for pointing that out
    &
    @Bob – Thanks man! I’ve added it and that fixed it! Should have looked at that earlier. Let me know if there are any other issues.

  34. Eddie
    Posted 30 May 2009 at 4:45 am | Permalink

    I can’t get externalInteface to work at all on ie if using Flash player 10 – works ok with other flash players. Anyone know a workaround or what is happening?

  35. Posted 17 November 2009 at 1:08 pm | Permalink

    I’m trying to use externalInterface to create a mouselistener so the mouse scroll will work when on top of the flash – as well as the spacebar and arrow keys working as well – any tips?

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>