Accessibility

TechNote (Archived)

An example of communication between Macromedia Flash 5 movies through JavaScript

Below are examples of communication between Macromedia Flash movies in an HTML document using JavaScript, followed by steps to recreate each.

This TechNote builds on information found in An example of communication between JavaScript and Flash (TechNote 15683), which explains how to usefscommand and Flash's JavaScript methods to send messages from a Flash movie to the host environment (Web browser) and vice versa. Below, these two techniques are combined to facilitate communication between two separate Flash movies in the same HTML page. Experienced scripters should first read the article Scripting with Flash for an overview of JavaScript methods which can control the Flash Player.

Note : Browser scripting is not a feature of all browsers. In order to communicate with the Flash Player the browser must have built in hooks which the Flash Player can 'listen' to. This is especially problematic for Internet Explorer on the Macintosh. For details see Flash and Internet Explorer Issues on the Macintosh Platform (TechNote 13638) as well as Netscape 6, 6.01 and Mozilla (Technote 14625). Specifically, these examples and techniques require:

Netscape Navigator 3.0 to 4.7x (LiveConnect and Java-enabled; Windows 95/98/NT/2000/XP or MacOS)
Netscape 6.2 or higher (LiveConnect and Java-enabled; Windows 95/98/NT/2000/XP or MacOS)

Internet Explorer 3.0 and above (ActiveX enabled; Windows 95/98/NT/2000/XP only)

Note: Browser scripting for Netscape 6.2 and above requires theNAME attribute of the Object tag to be removed. Netscape 6.2, 7 or higher will not allow script communication if the NAME parameter is in the Object tag. The ID attribute of the Object tag is still required for Internet Explorer compabitility (and is ignored by Netscape).

Flash-to-Flash communication
In his example, text entered in one Flash movie is sent to another Flash movie embedded in the same HTML document.

SeeExample 1 to view a working demonstration of this technique.

Download the source files for this example flash_to_flash.zip (24K).

In Flash

Create the first Flash movie, which will contain an input text field and a button.
1

Create a new file in Flash and save it as
send_to_flash.fla.

2

Add a text field to the stage using the Text Tool.

3 Select the text field and open the Text Options panel (Window> Panels> Options). 4

In the Text Options Panel type the variable namemyVar into the Variable field and hit the Enter key, choose Input Text from the Text Type pop-up menu and select the Bg/Border option.

5 Add a button to the Stage. (You can find a sample button by choosing Window> Common Libraries> Buttons.) 6 Select the button instance and choose Windows> Actions to open the Actions panel. 7

On the Object Actions panel for the button paste or add the following actions:

on (release) {
fscommand ("send_to_flash", myVar);
}

8 Save the file. 9

Choose File> Export Movie and export the file asmovie1.swf . Remember the name and location of the folder to which you export this SWF.

Create the second Flash movie, which will contain a dynamic text field to display the value entered in the first movie.
1

Create a new file in Flash and save it asreceive_from_flash.fla.

2

Place a text field on the stage using the Text Tool.

3 Select the text field and open the Text Options panel (Window> Panels> Options) 4

In the Text Options Panel type the variable namedisplayVar into the Variable field and hit the Enter key, choose Dynamic Text from the Text Type pop-up menu and select the Bg/Border option.

5

Save the Flash file.

6

Choose File> Export Movie and export the file asreceive_from_flash.swf into the same folder that containssend_from_flash.swf .

In Dreamweaver 4.0
1

Open a new HTML file by choosing File> New. Save the document to the same folder that contains the Flash movies created above.

2

Choose Insert> Media> Flash and insertsend_from_flash.swf. Repeat the same Insert menu command, this time inserting receive_from_flash.swf .

3

In the Document window, select the placeholder area forsend_from_flash.swf and open the Property Inspector (Window> Properties). In the Name and ID fields enter the valuesend_to_flash , as shown below.



Note: For Netscape 6.2 and above this functionality requires the NAME attribute of the Object tag to be removed. Netscape 6.2, 7 or higher will not allow script communication if the NAME parameter is in the Object tag. The ID attribute of the Object tag is still required for Internet Explorer compabitility (and is ignored by Netscape). 4 Select the placeholder forreceive_from_flash.swf and, also in the Property Inspector, enter the valueget_var into the same Name and ID fields. 5 Launch the Code Inspector, or switch to Code view, to view the HTML source code. 6

In the HTML source, add theswliveconnect=true attribute to the<embed> tags for both Flash movies, as shown below.

 <embed href="/support/flash/ts/documents/filename.swf" quality=high bgcolor=#FFFFFF width=300 height=200 swliveconnect=true> 

Note : The example will not work in Netscape browsers unless theswliveconnect attribute is present.

7

Paste the following JavaScript code into the HTML source directly after the opening<body> tag:

 <SCRIPT LANGUAGE=JavaScript><!-- var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1; function send_to_flash_DoFSCommand(command, args) {         window.document.get_var.SetVariable("displayVar", args); } if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {         document.write('<SCRIPT LANGUAGE=VBScript\> \n');         document.write('on error resume next \n');         document.write('Sub send_to_flash_FSCommand(ByVal command, ByVal args)\n');         document.write(' call send_to_flash_DoFSCommand(command, args)\n');         document.write('end sub\n');         document.write('</SCRIPT\> \n'); } //--></SCRIPT> 
8

Choose File> Save.

9

Choose File> Preview in Browser and choose a browser, or press F12.

Additional information
For more detailed information about making the communication between Flash and JavaScript, please consult An example of communicating between Javascript and Flash (TechNote 15683)



AlertThis content requires Flash

To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.

Download the free Flash Player now!

Get Adobe Flash Player

Creative Commons License

Search Support


Document Details

ID:tn_15692

Products Affected:

flash
flashplayer