Showcase and discover digital art at yex

Follow Design Stacks

Subscribe to our free newsletter to get all our latest tutorials and articles delivered directly to your inbox!

Communication methods prior to Flash 8

Communication methods prior to Flash 8

Earlier versions of Flash Player 8 relied on getURL() and fscommand() to communicate from Flash to JavaScript, and on SetVariable() to communicate from JavaScript to Flash.

Using getURL()

The easiest way to call JavaScript from Flash is by using getURL(). To call callJavascript() from Flash, use the following ActionScript:

var msg:String = "''Hello. ^^'";
var callJas:String = 'javascript:callJavascript('+msg+')';

In HTML, type the following JavaScript function between the <head> and <body> tags :

<script language="JavaScript"> 
function callJavascript(str){

In Flash, call the callJavascript() function and send the message string “Hello. ^^” as the parameter. The result appears in Figure 1.

Alert window generated from Flash

Figure 1. Alert window generated from Flash

Note: If your SWFs throw security errors when tested locally in Flash Player 8, change allowScriptAccess="sameDomain" to allowScriptAccess="always". In addition, you may need to allow access to the SWF file in the Global Security Settings panel.

Using fscommand()

The fscommand() command lets the SWF file communicate with either Flash Player or the program that is hosting Flash Player, such as a web browser. For example, the following ActionScript would call the JavaScript function callJavascript():

var msg:String = "'Hello. ^^'";
var callJas:String = "callJavascript";
fscommand(callJas, msg);

Open the Publish Settings dialog box (press Control+Shift+F12) in Flash. On the HTML tab, select Flash with FSCommand in the Template pop-up menu (see Figure 2) and click the Publish button.

Specifying the publish settings

Figure 2. Specifying the publish settings

If you look at the HTML page that is published, the following JavaScript has been automatically included:

<script language="JavaScript">
var isInternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
// Handle all the FSCommand messages in a Flash movie.
function fscommand_DoFSCommand(command, args) {
var fscommandObj =
isInternetExplorer ? document.all.fscommand : document.fscommand;
// Place your code here.
// Hook for Internet Explorer.
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"> ');
document.write('On Error Resume Next ');
document.write('Sub fscommand_FSCommand(ByVal command, ByVal args) ');
document.write(' Call fscommand_DoFSCommand(command, args) ');
document.write('End Sub ');
document.write('</script> ');

Your custom code goes in the section “Place your code here.”  Specifically, this code needs to receive and process the command and args parameters sent from Flash using fscommand().

For example, the following code creates an alert window by handling command and args:

function fscommand_DoFSCommand(command, args) {
var fscommandObj =
isInternetExplorer ? document.all.fscommand : document.fscommand;
if(command=="callJavascript") {

Because fscommand() uses VBScript under the hood to communicate, it may cause conflicts with other VBScript on the page. Also, because fscommand() passes only one args string parameter, you must use the split() function in JavaScript to pass two or more parameters.

The following code is an example of using fscommand() to pass two arguments. In Flash, type the following code:

var msg:String = "'Hello. ^^'#Glad to see you.*^^*";
var callJas:String = "callJavascript";
fscommand(callJas, msg);

Here, # serves as a separator between the arguments you want to pass. The JavaScript code is as follows:

function fscommand2_DoFSCommand(command, args) {
var fscommand2Obj =
isInternetExplorer ? document.all.fscommand2 : document.fscommand2;
if(command=="callJavascript") {
arg = args.split("#")
alert(arg[0]+' '+arg[1])

In the code above, I separated the string on the # separator and used
to wrap text onto a new line in the alert window. The result appears in Figure 3.

Alert window displaying both arguments

Figure 3. Alert window displaying both arguments

Note: As in the previous examples, to prevent security errors from being thrown in Flash Player 8 when you test locally, change allowScriptAccess="sameDomain" to allowScriptAccess="always".

Using SetVariable()

The only way to communicate with Flash in JavaScript is by using one of a few predefined methods. SetVariable() is one of the most common methods to use. It can send string values from JavaScript to a SWF that is embedded on the page. Then, in ActionScript, you can use the watch() method to invoke a callback function whenever new values are passed in.  Let’s look at an example.

Type the following code in Flash:

 // An  empty string variable
var testValue:String = "";
// The callback function to be executed when the variable changes
watchCallback = function (id, oldval, newval):String {
// Here you place the new value in a text field for display
result_txt.text = newval;
return newval;
};"testValue", watchCallback);

The above code invokes watchCallback() whenever the testValue value changes. When watchCallback() is invoked, the new value is assigned to result_txt.text on _root.

Insert the following code in the HTML <body> section:

<input type=button value="Pass 
the variable to Flash" onClick="callJavascript()">
<form name="inputValue">
Value <input type="text" name="vars" size="10" >

The value entered in the input text in HTML is saved in the vars property of the form inputValue. Clicking the “Pass the variable to Flash” button invokes the callJavascript() function.

The following is the JavaScript code:

<script language="JavaScript"> 
function callJavascript(){
var sendText = inputValue.vars.value
window.document.myMovie.SetVariable("testValue", sendText);

In the code above, window.document.FlashID gives you a reference to your Flash object (which I haven’t shown here). You then call SetVariable(variable, value) on the reference to pass the value of sendText in a variable named testValue

window.document.setVariable.SetVariable("testValue", sendText); 

Figure 4 shows the results.

Pass a value to Flash via JavaScript on the HTML page

Figure 4. Passing a value to Flash using JavaScript on the HTML page