Flash and JavaScript

[ LiB ]

Scripting Flash movies in the browser follows the same rules as scripting any plug-in media type (see Chapter 15, "Plug-Ins, ActiveX, and Java," for more on this). Whenever you want Flash movies to talk to the browser or the browser to give commands to the Flash Player, you use Flash ActionScript to send JavaScript instructions, or JavaScript to send ActionScript code.

Scripting In: Using JavaScript to Control Flash

A variety of JavaScript commands exist for communicating with the Flash Player, in its plug-in and its ActiveX forms. Table 16.6 lists some of the most commonly used of them. As you can see from examining the figure, it's possible to send the Flash movie to a certain frame in its timeline, to start and stop playback, and even to control embedded movie clips by setting their properties and controlling their timelines .

Table 16.6. A Selection of JavaScript Methods Available for Controlling the Flash Player






Starts playing the specified movie.



Stops playing the specified movie.



Sends the movie to its first frame.


myMovie.GotoFrame ( frameNumber )

Sends the movie to a specified frame number.


myMovie.Zoom ( percent )

Zooms the view by a factor() specified by percent . Numbers smaller than 100% increase the magnification.


myMovie.SetZoomRect (left, top, right, of the movie. Values are integers bottom)

Zooms in on a rectangular area representing twips (1440 twips per inch, 17 twips per point).


myMovie.SetVariable ( varName , value )

Sets the value of a specified Flash variable. Both arguments are strings.


var a = myMovie. GetVariable ( varName )

Returns the value of a specified Flash variable as a string.

TCallFrame() , TCallLabel()

myMovie.TCallFrame ( target , frameNumber ) myMovie.TCallLabel ( target , frameLabel )

In the target timeline, executes any frame actions in the specified frame. (Similar to the Flash call() method.)


myMovie.TPlay ( target )

Plays the specified movie clip.


myMovie.TStopPlay ( target )

Stops playing the specified movie clip.

TGotoFrame() , TGotoLabel()

myMovie.TGotoFrame ( target , frameNumber ) myMovie.TGotoLabel ( target , frameLabel )

Sends the timeline of the specified movie clip to the specified frame.

TCurrentFrame() , TCurrentlabel()

var a = myMovie. TCurrentFrame ( target ), var b = myMovie. TCurrentLabel ( target )

Returns the number or label of the current frame for a specified movie clip.

(The more you know about Flash authoring, the more useful this information will be to you.) Before relying on any of these commands, remember that although ActiveX media controls support extensive scripting for IE/Windows, no commands given to media objects will work within IE/Mac. Netscape 6 also has some difficulties passing commands to Flash movies.

Not all methods work with the Flash plug-in. For a complete list of supported JavaScript methods, visit www.macromedia.com/support/flash/publishexport/scriptingwithflash/scriptingwithflash_03.html.

Scripting Within Dreamweaver: The Control Shockwave or Flash Behavior

For basic Flash control using JavaScript, the Control Shockwave or Flash behavior lets you use HTML page elements to start, stop, rewind, and send movies to specific frames without writing a lick of code. (Note that the only reason to use this behavior is if you want non-Flash page elements to control the movie. Flash movies are capable of containing their own internal buttons that will start, stop, and so on. If the movie contains its own interactivity, you don't need to add any JavaScript in Dreamweaver.)

To control a Flash movie using this behavior, follow these steps:

  1. Insert a Flash movie in your document.

  2. Give the movie a one-word name. You can do this in the Flash Property Inspector by filling in the name field in the upper-left corner.

  3. Decide what sort of page element and event you want to trigger the action (when the user clicks a text link, when the page loads, and so forth).

  4. Select the element that should trigger the action (page, text link, and so on), and use the Behaviors panel to apply the behavior. Figure 16.18 shows the behavior being chosen and the resulting dialog box with its various options.

    Figure 16.18. Choosing and configuring the Control Shockwave and Flash behavior.

As its name implies, the Control Shockwave or Flash behavior also can be used to control a Director Shockwave movie. The same procedure outlined here would apply. (See Chapter 15 for more on working with Shockwave.)

Scripting Out: Using Flash to Send JavaScript Instructions

Just as you can (theoretically, anyway) send scripting messages from the browser to the Flash Player, you can send messages from the Flash movie to the browser. Because this is done in Flash, not in Dreamweaver, it's beyond the scope of this discussion. But it's handy knowledge to have anyway. Just as in the preceding exercise you used javascript: in a link field to send a JavaScript command in to Flash, Flash uses the same syntax in its getURL link command to send out commands.

Figure 16.19 shows the Flash file gecko_helloworld.swf with this link in place. To see how the link works, find that file in the chapter_16 folder, place it in an HTML document, and preview it in a browser. Note that because this is JavaScript coming out of a Flash movie instead of going in, it should work much more reliably in the different browser/platform combinations.

Figure 16.19. The gecko_helloworld.swf file being created in Flash.

[ LiB ]

Macromedia Dreamweaver MX 2004 Demystified
Macromedia Dreamweaver MX 2004 Demystified
ISBN: 0735713847
EAN: 2147483647
Year: 2002
Pages: 188
Authors: Laura Gutman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net