Marius van Witzenburg We fight for our survival, we fight!

14jun/110

How to control a Flash movie from Javascript and use Javascript from Flash movies

Posted by mariusvw

Personally I dislike the usage of Flash in my websites but our clients request a lot that they want animations. Now it happens that they want the animation to act upon an action to a non-flash object. This can be difficult if you have no idea how to do that. If you read below, you should have no problem with getting this job done :-)

Control your Flash Movie from Javascript:

// Play movie
document.getElementById('myFlashMovie').Play();
// Stop movie
document.getElementById('myFlashMovie').StopPlay();
// Rewind movie to beginning
document.getElementById('myFlashMovie').Rewind();
// Goto next frame of MovieClip
document.getElementById('myFlashMovie').TGetProperty(nameOfTargetMovieClip, propertyIndex);
// Goto next frame of TimeLine
document.getElementById('myFlashMovie').GotoFrame(frameNum);
// Zoom in or out the flash movie
document.getElementById('myFlashMovie').Zoom(relative percentage);
// Set variable and make it available via ActionScript
document.getElementById('myFlashMovie').SetVariable(variableName, variableValue);
// Get variable for usage in JavaScript
var myVariable = document.getElementById('myFlashMovie').GetVariable(variableName);

To control Javascript functions you can call it like this:

ActionScript 1/2

getURL("javascript:name_of_your_function();");

ActionScript 3 direct call

var url:String = "javascript:name_of_your_function();";
var request:URLRequest = new URLRequest(url);
try {
  navigateToURL(request);
} catch (e:Error) {
  trace("Error occurred!");
}

ActionScript 3 event mouse click call

movieClipName.addEventListener(MouseEvent.CLICK, callLink);
function callLink:void {
  var url:String = "javascript:name_of_your_function();";
  var request:URLRequest = new URLRequest(url);
  try {
    navigateToURL(request);
  } catch (e:Error) {
    trace("Error occurred!");
  }
}

For some more information about these methods, please check out the Adobe website.

I hope this helps you out with getting that Flash Movie behave like you want it to ;-)