You Are Here Home > Calling a JavaScript function from Actionscript 3 (Flash)

Calling a JavaScript function from Actionscript 3 (Flash)

It’s very easy:

import flash.external.ExternalInterface;
...
ExternalInterface.call("your_javascript_function()");

You can even get a return value:

var x:int = ExternalInterface.call("get_x()");

To pass an argument try:

var retval:int = ExternalInterface.call("some_js_function()", "the-argument");
Calling a JavaScript function from Actionscript 3 (Flash)
Filed under: Actionscript, Flash, JavaScript, Web Development   Posted by: Codehead

Got a Question?

Get answers here.

35 Comments »

  1. Taha:
     

    Nice Man.. Thanks

    Comment

  2. Pierre:
     

    Cheers awsome man, thanks!

    Comment

  3. Kevin:
     

    Good to know, thanks.

    Comment

  4. farid:
     

    hi man ,
    i think this is wrong syntax !!!!!!!!!!
    ExternalInterface.call(“your_javascript_function()”);
    because here you passed 0 arguments to the function , and when we want to pass arguments to external interface function we are using
    ExternalInterface.call(“your_javascript_function”,”red”);
    not
    ExternalInterface.call(“your_javascript_function(red)”);

    ——————————
    so i think if you would not to pass any aruments to function you should write
    ExternalInterface.call(“your_javascript_function”);
    without ===> “()”
    ————————–
    cheers ;)

    Comment

  5. Codehead:
     

    Well, it’s not “wrong syntax”, () works fine, traditionally it’s associated with functions, even in JavaScript when you use setTimeOut if you include () it will work just fine…

    Comment

  6.  

    :) Cool, Thanks.

    Comment

  7.  

    Pro. Very helpful for my API for my site/flash games..!

    Comment

  8. Moatist:
     

    Thanks man!

    Comment

  9.  

    Thanks a lot for this useful article. But I had difficult time navigating past your web site because I kept getting 502 bad gateway error. Just thought to let you know.

    Comment

  10. Ruth Ruthy:
     

    Your great tip might lead to me getting a full night’s sleep! Thank you! I have a question, just to make sure I’m getting this right.

    If I put the following AC3 code on the very last frame of my Flash movie will it call my JS function correctly?

    import flash.external.ExternalInterface;
    ExternalInterface.call(“javascript:parent.splashpage.closeit()”);

    Thank you in advance for your assistance
    -RR

    Comment

  11. Codehead:
     

    Ruth, honestly I don’t know, the best way to find out is to try it I guess :)

    Comment

  12.  

    Whoa, that was way easier than what I’ve been trying to do
    Now…
    What if I wanted to call that Javascript from a click or mouse over from the Action Script?

    Comment

  13. Auzzie:
     

    Hello,
    Stumbled across this post searching for another solution and it helped me a lot.

    I was trying to apply it to a separate issue that involved a jquery script and was wondering if this solution would work for that.

    The as3 needs to pass an anchor tag string to the jquery to initiate a smooth scroll script.

    This is the script that controls the scroll:

    $(document).ready(function(){
    $.smoothAnchors(3500, “swing”, true);
    });

    Would this solution work?

    Comment

  14. Codehead:
     

    You can always wrap the jQuery script in a function and call that function from Actionscript…

    Comment

  15. Casius:
     

    Hi People!
    I have some problem: my flash get a variable (Link) from html and use it for a url, i get by example: Link=”www.site.com” and then i use

    goToURL(Link, “_blank”);
    function goToURL(URL, sType:String) {
    var request:URLRequest = new URLRequest(URL);
    try {
    navigateToURL(request, sType);
    }
    }

    that works fine for “simples” url or “.php” but i cannot execute a function in javascript like:
    Link=”ExternalInterface.call (‘alert’,'Esta info!’)”

    Any suggestion?

    Comment

  16. Codehead:
     

    ExternalInterface.call (’alert’,’Esta info!’) is not a URL that you can pass as a string, you must execute/call ExternalInterface.call (’alert’,’Esta info!’)

    Comment

  17. Casius:
     

    Well, i finally found the way to pass a variable from html to flash to execute some function in javascript, this is sLink=”javascript:alert(‘Info!’)”;

    Comment

  18. SeriousSamP:
     

    HI,
    I’m just learning AS3 and am working on a project that uses flash to change the contents of iFrames using JavaScript. I tried using your code to call the script but it goves the error

    1084: Syntax error: expecting identifier before tripledot.

    What am I doing wrong?

    Comment

  19. SeriousSamP:
     

    OK I take that back works without the tripledot. Thanks man great tut.

    Comment

  20. Marcone:
     

    Hi there.

    When u say:

    var x:int = ExternalInterface.call(“get_x()”);

    U mean get_x() being a function returning some value? Is it possible to recover multiple variables from a single function?

    Thanks in advance.
    Marcone

    Comment

  21. Codehead:
     

    Marcone, I honestly didn’t ever try this but if I wanted to do this I would start experimenting with returning an array from the JS function instead of a single value…

    Does that make sense?
    :)

    Comment

  22. Max:
     

    Greetings, Hamid!
    How to receive “url” a hosting from which start SWFfile (itself SWFfile to be on other host)…

    More in detail:
    - Is “host.com” on which it is stored SWF…
    - There is an unknown person second_host_name.com which starts it (SWF)
    - How to learn second_host_name? (AS3 tools)

    If it is possible more in detail!
    Thanks, yours faithfully!

    Comment

  23. Codehead:
     

    Max, there is an easy way to do this, you go to your hosting package’s logs > Recent Visitors (or similar) and find the SWF file, then look at the ‘HTTP Referer’ and you will see the page in which this file is used.

    (I’m not sure about this one) The other thing is you probably could check for the HTTP Referer header inside the SWF file and send it to a PHP script…

    Sorry I don’t have a lot of time to explain now but use the first method or Google the second one.

    Good Luck :)

    Comment

  24. Max:
     

    Thx CodeHead!
    With PHP there is no possibility to connect, eat only loaded SWF unfortunately, and all…

    It is a pity that there is no time… :( Your help would help Me!
    If there will be time, I ask you, give some minutes…

    Your logic is clear, the only thing that not absolutely it turns out to carry out it

    //
    if (flash.external.available)
    try {
    var win:* = flash.external.call (“window.location”);
    }
    //

    Does not return (URL)… Do not know why?

    Thanks, yours faithfully!

    Comment

  25. Codehead:
     

    The problem is that window.location is not a function and the URL is actually in window.location.href so try:

    var url:String = ExternalInterface.call('function(){return location.href}');

    Or:

    var url:String = ExternalInterface.call("window.location.href.toString");

    Again, sadly I don’t have time to try these…

    Comment

  26. Max:
     

    Thx, Hamid! I try!

    Comment

  27. Marco:
     

    Thanks for the info, very helpful. But I was getting “undefined” errors when the argument was passed to the function in JavaScript. You say that having parenthesis after the function name is okay, but it’s not if an argument is added (it’s okay if no argument is present). Removing the () cured the undefined errors I was getting.

    So instead of
    ExternalInterface.call(“some_js_function()”, “the-argument”);

    It should be
    ExternalInterface.call(“some_js_function”, “the-argument”);

    The Adobe ref set me straight:
    http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/external/ExternalInterface.html

    Comment

  28. ruchira:
     

    where will be that javascript function?
    wether in seperate file?
    reply please i need dat

    Comment

  29. Codehead:
     

    The function should be in the same page that includes the Flash (I think), it should be in the same page or one of the included JavaScript files using:

    <script src="..." ...> </script>

    Comment

  30.  

    Javascript is as permisive as AS1, it would have been nice that browsers implemented ECMA Script 4, strict typing rocks! then javascript would trully replace flash. Still prefer flash

    Comment

  31. Jason:
     

    hi, this is a great help to my project!

    Comment

  32.  

    This is exactly what I need to hide my Flash movie once it finishes! Thanks!

    Comment

  33. shirty dogg:
     

    @Codehead and @ruchira:
    The js stuff has to be in the same page anyway. What you really mean ‘the same page’? I’m pretty sure the flash object cannot perform that action before document.onready function is fired so your js function can be on very bottom on the page, can be in the frame(s) but this is the same rule to call. If you wish a new js code can be passed trough ajax and then evaluated so you can use it with the flash even this way(needs a precognitive abilities). You can even store your js code in the flash movie, append to DOM after swf is loaded and then use it or just use the lambda functions all the time as Codehead said before. Cheers ;)

    Comment

  34. Sérgio:
     

    hey guys, a tip for all who use JQuery like me:
    if you define functions inside the: $(document).ready(); in your javascript file, it’s don’t work with the External of Flash.
    Define your functions in other file, without the $(document).ready(), only with the function.
    =)

    Comment

  35.  

    I NEED TO AD
    window.addEvent(‘domready’, function(){
    var overlay = new overlay();
    var box = new multiBox(‘mb’, {
    overlay: overlay
    });
    });

    but i get 1120: Access of undefined property window.

    Comment

RSS feed for comments on this post. TrackBack URL

Leave a comment