• flickr
  • linkedin
  • twitter
  • rss
Call AS3 functions from javascript and vice versa

Call AS3 functions from javascript and vice versa

To communicate between JavaScript and Actionscript it isn’t so hard.


import flash.external.ExternalInterface;
ExternalInterface.addCallback("sendTextToFlash", getTextFromJavaScript);

function getTextFromJavaScript(str):void {


 var currentPage="Home";
    function setCurrentPage(newPage) {
        currentPage = newPage;

So then on the page, this code is run when an item is clicked (javascript function sending to flash)

<a href="javascript:void(0);" onClick="setCurrentPage('Home')">Home</a>

This javascript function is to return the reference to the flash movie obj box in whatever browser:

function getFlashMovieObject(movieName){
if (window.document[movieName]){
return window.document[movieName];
if (navigator.appName.indexOf("Microsoft Internet")==-1){
if (document.embeds && document.embeds[movieName])
return document.embeds[movieName];
return document.getElementById(movieName);

and then finally, to send the vars etc. to flash, crank it like so:

function SendDataToFlashMovie(newPage){
var flashMovie=getFlashMovieObject("main_flash");

This is all blatantly ripped from Painteddigital (just incase they take it down)

SO THEN. To communicate your actionscript to javascript, it’s really easy – just navigateToURL it:

navigateToURL(new URLRequest(String("javascript:openWindow('modules/" + listing[targetID] + "')")), "_self");

bit easier below:

It’s very easy:
import flash.external.ExternalInterface;

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”);


Tech Reference: ,

Larry's a 30-something chap interested in tramping, being a dork and changing the world via less boring training.

Leave a reply