I first needed this trick when I had an ActionScript 2.0 file and an ActionScript 3.0 file that needed a little chat. Subsequently I have used this more and more to have my jQuery magic speak to my ActionScript magic like two school boy wizards. Firstly in the Javascript let’s identify the Flash object we want to talk to:

function getFlashMovie(movieName) {
   var isIE = navigator.appName.indexOf("Microsoft") != -1;
   return (isIE) ? window[movieName] : document[movieName];
}

This function needs to be passed the ID of the Flash object ie getFlashMovie(‘myFlash’). Now we can pass a call to the ActionScript. In the javascript below our call is labelled ‘startMe’ :

getFlashMovie("iLoader").startMe();

If you need to pass parameters to your ActionScript then do so as follows:

getFlashMovie("iLoader").startMe(6);

In your ActionScript you will need to import the ExternalInterface class.

import flash.external.ExternalInterface;

In ActionScipt the ExternalInterface method addCallback is the bridge. The method basically makes an ActionScript function callable from your Javascript call. The two parameters addCallback requires are firstly your Javascript call and secondly the ActionScript function you are making externally callable.

ExternalInterface.addCallback("startMe", startMovie);

The following ActionScript method is set up to receive the variable from Javascript. This is optional.

        public function startMovie(valFromJS:Number):void{
		// do it
                trace(valFromJS);
	}

You can view a demo here.

Leave a Comment