Controlling the Flash Player with JavaScript

ActionScript is the scripting language within Flash that can be used to control the playing of the movie. It is similar syntactically to JavaScript and provides for extensive control of the FlashPlayer. However limited control of the FlashPlayer is possible through JavaScript. The FlashPlayer object can be accessed through getElementById and the methods of this object all begin with a capital letter. The following code illustrates the procedure:

<object id="ball" ...><embed id="ball" ... ></embed></object>
<form>
<input type="button" value="Stop" onclick="document.getElementById('ball').StopPlay();">
<input type="button" value="Play" onclick="document.getElementById('ball').Play();">
</form>


For example, one can attach JavaScript to the event handlers in HTML buttons to control the playback of the movie. The Stop button above uses the StopPlay() method and the Play button uses the Play() method. Other methods include IsPlaying(), Rewind(), PercentLoaded(), Zoom(), Pan(),GotoFrame(integer frame number) (goes to the frame and stops). There does not appear to be a method to play for a specified number of frames.

ActionScripting would provide for more complete control of the Flash movie and there are also ways for JavaScript and ActionScript to communicate with each other).

Revised: May 3, 2007. Reference: JavaScript The Definitive Guide, 5th Edition, O'Reilly, 2006, pp. 582-584, 800-806.