How to Retrieve Internal SWF Variables

Flash file URLs can include variable data.
... Comstock/Stockbyte/Getty Images

When you use SWF Flash files within websites and Web applications, you can pass variables into them from the Web page. You can also pass data between SWF files. To retrieve variables inside a Flash file, you need a small excerpt of ActionScript code. Flash files can therefore retrieve variables from the file URL, from the page HTML or JavaScript code, or from another Flash file. The process for retrieving variables inside a Flash file depends on where the variables are coming from.

Pass the variables into the SWF. You can pass variables into Flash in a number of ways. Append a variable to the SWF filename when it is loaded into the Web page as follows:

moviefilename.swf?info=hello

Include the variable as part of an embed element in HTML as follows:

FlashVars="info=hello"

If you are passing variables from one Flash file to another, use the following ActionScript syntax:

var varConnection:LocalConnection = new LocalConnection(); varConnection.send("your_connection", "method_in_swf", "hello");

This would work where the SWF you are sending the variable to has a method named "method_in_swf" inside it.

Retrieve variables in the target SWF. Open the target Flash file in Adobe Flash Professional. If the file does not already contain a layer for ActionScript code, create one now. Open the Actions panel. To retrieve variables either attached to the filename or passed as FlashVars, use the following:

var infoVar:String = root.loaderInfo.parameters["info"];

The parameters are stored as an array, so you can include a parameter name to retrieve its value, which in this case will be "hello." Alternatively, you can achieve this using the following slightly different syntax:

var infoVar:String = root.loaderInfo.parameters.info;

To retrieve variables sent using the LocalConnection class, include a parameter in the method you called. For example:

function method_in_swf(info:String){ //access the variable from the "info" parameter }

Use the variable you retrieved. Once you have retrieved a variable inside a Flash file, you can use it as you would use a variable you defined in the file. For example, you may wish to write the variable out for display to the user or use it to determine how your SWF functions.

  • Whenever your SWF files use external data, there is a danger that the data will not be retrieved, so take steps to ensure that the movie continues to function if this happens.
  • You can control the ActionScript functions in an SWF from JavaScript in the HTML page, giving you more options for interaction between the page and your SWF movie.

Sue Smith started writing in 2000. She has produced tutorials for companies including Apex Computer Training Software and articles on computing topics for various websites. Smith has a Master of Arts in English language and literature, as well as a Master of Science in information technology, both from the University of Glasgow.

×