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");
Hamid Alipour is a partner in Codehead, LLP with his wife, Tess. Hamid speaks 12 markup and programming languages [Yes, 12: PHP, CSS, Ajax, JavaScript, HTML/XHTML, Java, Python, C/C++, ASP, Visual Basic, Scheme and Action Script]; has a penchant for solving the unsolvable; an affinity for clean, hand-written code and is a Zend Certified 
Nice Man.. Thanks
Comment
Cheers awsome man, thanks!
Comment
Good to know, thanks.
Comment
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
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
Comment
Pro. Very helpful for my API for my site/flash games..!
Comment
Thanks man!
Comment
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
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
Ruth, honestly I don’t know, the best way to find out is to try it I guess
Comment
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
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
You can always wrap the jQuery script in a function and call that function from Actionscript…
Comment
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
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
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
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
OK I take that back works without the tripledot. Thanks man great tut.
Comment
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
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
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
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
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
The problem is that window.location is not a function and the URL is actually in window.location.href so try:
Or:
Again, sadly I don’t have time to try these…
Comment
Thx, Hamid! I try!
Comment
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
where will be that javascript function?
wether in seperate file?
reply please i need dat
Comment
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:
Comment
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
hi, this is a great help to my project!
Comment
This is exactly what I need to hide my Flash movie once it finishes! Thanks!
Comment
@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
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
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