Subscribe to Tutorial Feed

Flash and PHP Bible

The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.

The Flash and PHP Bible has a forum for quick support.

Scriptplayground » tutorials » as » Call Javascript in Flash

Call Javascript in Flash

Run Javascript in Flash without the need of extra code in your HTML file.

Have you ever needed to test some variables or call a function from in Flash? Well here is the answer.

function runJS(script:String):Void {
	getURL("javascript:" +  script + "");
}

var js = "var doc = document.URL; alert(doc);";
runJS(js);

That example block of code is basically a function that handles a call to "getURL" which actually executes the Javascript. This example displays in a message box the current URL of a document. Which of course will only work if you are previewing the SWF in a web page or Flash will attempt to open your default web browser.

There isn't a lot to this idea, just makes it a little easier to run Javascript in Flash.

Follow Scriptplayground on Twitter (@scriptplay)

Comments: Call Javascript in Flash

 AsMDaR  Tue Apr 29, 2008 11:57 am  
Cool! thanx!
 Jorge   Thu Aug 28, 2008 3:53 am  
Hi sir,

Can't make it work, does this need plugins or anything??
Please help, need it badly.
 mkeefe  Fri Aug 29, 2008 5:57 am  
@Jorge - Depends on the browser/environment. What (if any) errors do you get?
 Sara Blake  Mon Dec 8, 2008 3:30 am  
Hey thanks!

Do you think you could give a bit more explanation? I need to call a Javascript function that makes my anchored links scroll, but some of those links are controlled from Flash buttons. Where do I specify the Javascript function's name in the AS?

My apologies if this sounds ridiculous, I'm learning.

Thanks!
 mkeefe  Tue Dec 9, 2008 8:20 am  
@Sara - Not all Javascript will work in Flash, however I recommend you take a look at ExternalInterface which allows more advanced opportunities and is supported with ActionScript 3.
 Jeff  Mon Dec 22, 2008 11:45 pm  
getURL() will popup another page...
 mkeefe  Sat Jan 10, 2009 12:00 pm  
@Jeff - It can, which in that case I would recommend the use of ExternalInterface.
 DJJaxe  Tue Jan 13, 2009 10:27 am  
as a tip getURL() only works in as2 not as3 in as3 it would end up being:


function runJS(script:String):Void {
var myURL:URLRequest = new URLRequest("javascript:" + script + "");
navigateToURL(myURL);
}
var js = "var doc = document.URL; alert(doc);";
runJS(js);


(sorry i didnt test this because i am at school right now so it might or might not work.)
 Morne  Mon Mar 9, 2009 9:37 am  
Thank you soooooo much!
 Sean  Fri Jul 24, 2009 7:50 pm  
Hi, Can I use JavaScript (Prototype.js and Scriptaculous) from a flash navigation? The rest of the page is XHTML and CSS.

thanks
 mkeefe  Mon Aug 3, 2009 10:03 am  
@Sean - Not sure what element of those libraries you want to use, but you should be able to call any proper function.
 gorka  Fri Aug 14, 2009 2:05 pm  
great shot!
 Joel  Sun Aug 30, 2009 6:40 am  
How do I call jQuery function with this code block? ..this is the jQuery code in an external .js file:

$(document).ready(function() {
$("div.panel_button").click(function(){
$("div#panel").animate({
height: "750px"
})
.animate({
height: "550px"
}, "fast");
$("div.panel_button").toggle();

});

$("div#hide_button").click(function(){
$("div#panel").animate({
height: "0px"
}, "fast");


});

});

and I'd like to call the first animation function from flash. Thanks.
 Stiofán  Wed Sep 2, 2009 11:17 am  
I haven't tested it, but it would probably be something like this...

function myNewFunction(){
$("div.panel_button").click(function(){
$("div#panel").animate({
height: "750px"
})
.animate({
height: "550px"
}, "fast");
$("div.panel_button").toggle();
});

$("div#hide_button").click(function(){
$("div#panel").animate({
height: "0px"
}, "fast");
});

}
$(document).ready(myNewFunction());
 Stiofán  Wed Sep 2, 2009 11:19 am  
Oh, then from your Flash it's

function runJS(script:String):Void {
getURL("javascript:" + script + "");
}

var js = "myNewFunction();";
_root.myButton.onRelease = function(){
runJS(js);
}


This demos how you run ANY JS function from Flash.
 Stiofán  Wed Sep 2, 2009 11:29 am  
But as Mkeefe says, the best way to do this is really to use the external Interface, in which case, you can run any function with...

/* MAKE FLASH WORK WELL WITH JS*/
import flash.external.ExternalInterface;
function clickSend(eventObj:Object):Void {
var jsArgument:String = sending_ti.text;
var result:Object = ExternalInterface.call("myNewFunction", jsArgument);
}
/* END MAKE FLASH WORK WELL WITH JS*/

_root.myButton.onRelease = function(){clickSend();}

Here, the only things you need to change are "myNewFunction" to whatever your function name is and the path to the Flash Button "_root.xxx". Easy peasy lemon squeezy.
 Alex  Fri Nov 27, 2009 10:55 pm  
great it will help me
 Stephan  Tue Sep 21, 2010 8:29 am  
Thanks for this post - Simple and just what I needed!
Add a comment
The ability to add new comments has been disabled in this archived version.

Loading
Main | Tutorials | Articles