Calling an ActionScript function from Flash
This is pretty cool… being able to control your Flash movies from your HTML page through JavaScript.
To do this, you need to make use of Flash’s ExternalInterface (from Flash 8+)
Your flash code will look something like this:
import flash.external.ExternalInterface;
function stopVideoPlaying() {
myVideo.stop();
}
ExternalInterface.addCallback("stopVideoPlaying", this, stopVideoPlaying);
The first line imports the External Interface. The next is three lines is the function we want to call from our HTML page.
Finally, we add an addCallBack method, which, in order, defines the JavaScript function to listen for, the target, and the function to call when the callBack is received.
The JavaScript code will look like this:
window.onload = function() {
if(navigator.appName.indexOf("Microsoft") != -1) {
flash = window.swfVideo;
}else {
flash = window.document.swfVideo;
}
}
function stopFlashVideo() {
flash.stopVideoPlaying();
}
Then, to call the Javascript method, just make sure your Flash is given an id name of swfVideo, then call the JavaScript function stopFlashVideo.