- 1). Set the following parameter between the "object" tags in the embed code of your HTML document:
<param name="allowScriptAccess" value="always" />
This will ensure that your Flash movie will be permitted to communicate with Javascript while you test it locally (see References 1, page 2). When you publish, you may change the value to "sameDomain" to prevent malicious use of your SWF file by third-party websites. - 2). Include the ExternalInterface class in the Actionscript code of your Flash file with the following command:
import flash.external.*;
(see References 1, page 5). - 3). Write a Javascript function in your HTML document that takes at least one argument and returns a value. You will not need to write any special Javascript code to call this function from Flash 8.
- 4). Use the ExternalInterface.call() method in your Actionscript to call your Javascript function from the embedded Flash movie. The first parameter of this method is the Javascript function's name as a string, and you can follow it with as many parameters as you want to pass outside to the function, all separated by commas. A general form of this call is:
ExternalInterface.call(FunctionName, Parameter1, Parameter2, ... )
(see References 1, page 5). - 5). Use a normal variable assignment operator in your Actionscript code to retrieve the return value from the Javascript function, just as you would with any function:
ReturnedValue = ExternalInterface.call(FunctionName, Parameter1, Parameter2, ... ); - 6). Use Actionscript's typecasting methods on the ExternalInterface call if you run into problems with the type of value Javascript returns. Int() and String() are typical examples (see References 2).
- 7). Use the addCallback() method of the ExternalInterface class to make methods in your Actionscript accessible to external Javascripts (see References 1, page 6). The addCallback() method has the following general form:
ExternalInterface.addCallback(ExternalName, Instance, InternalName)
ExternalName is the name by which the function may be called from Javascript, and InternalName is the name of the function in Actionscript. The "Instance" parameter is used to specify the resolution of the "this" keyword, and is usually set to "null". - 8). Call your Actionscript function from Javascript using the name you specified for "ExternalName" in the addCallback method, preceeded by a valid DOM reference to the Flash movie. For example:
window.document.flashMovie.flashFunctionName(Parameter1, Parameter2, ... );
See the Resources section for a more in-depth discussion of valid DOM references, and be sure to test your code in multiple browsers to ensure the Flash movie is referenced correctly.
SHARE