Table of Contents

loadVariables( ) Global Function Flash 4

retrieve an external set of variables
loadVariables(url, target)
loadVariables(url, target, method)

Arguments

url

A string specifying the path to a variable source�either a server-side script that returns variables or a text file containing variables.

target

A string indicating the path to the movie clip or document level on which the loaded variables will be defined. Can also be a reference to a movie clip or document level (references are converted to paths when used in a string context).

method

An optional string indicating the method by which to send variables to an external script. If specified, the variables from the current timeline are sent to the script and target receives the loaded variables. If omitted, variables are retrieved but none are sent. The legal values for method are "GET" and "POST". This parameter must be a literal, not a variable or other expression. On Windows, prior to Flash 6, the Standalone Player always used the "GET" method, regardless of the method specified. As of Flash 6, both "GET" and "POST" are supported. See Appendix F.

Description

Using loadVariables( ), we can import variables into a movie from a text file or a server-side application such as a Perl script. However, as of Flash 6, the LoadVars class provides a more flexible and powerful means of importing and exporting variables.

Variables loaded via loadVariables( ) are scoped to the movie clip or level specified by target and are always of the string datatype. To attach loaded variables to the current timeline, use the empty string as the value of the target argument. For example:

loadVariables("myVars.txt", "");  // Loads variables from myVars.txt
                                  // onto the current timeline

Whether the variables to be loaded reside in a text file or are composed by a script, they must be formatted according to the rules of URL encoding, described in the LoadVars class entry.

A text file for use with loadVariables( ) is simply a plain text file containing URL-encoded variables (as described in the LoadVars class entry). To load variables from an external text file, we specify the path of the file as the url argument in our loadVariables( ) function invocation. For example:

// Load the variables from myVariables.txt into the main movie timeline
loadVariables("myVariables.txt", "_root");

The loadVariables( ) function can also be used with a script or server application that outputs URL-encoded variables. When a script sends data to a Flash movie in response to a loadVariables( ) invocation, the script should set the MIME type of the data as: "application/x-www-form-urlencoded". Here's a line from a typical Perl script to set the MIME type:

print "Content-type: application/x-www-form-urlencoded\n\n";

Though the name loadVariables( ) suggests only a single direction of variable transmission, this function can also be used to send variables to a server-side script. To send all the variables defined on the current timeline to a script, we set the method argument of a loadVariables( ) function invocation to either "GET" or "POST". Variables are sent in URL-encoded format. If method is set to "GET", the variables are appended as a query string to url and sent to the remote script. If method is set to "POST", the variables are sent after the HTTP POST-request header. The "POST" method was not available in the Standalone Player for Windows prior to Flash 6, but it is functional in the Flash 6 Standalone Player for Windows (see Appendix F for details on support for GET and POST). Because most web servers restrict the length of URLs to no more than 1024 characters (and some to as little as 255 characters), use "POST" instead of "GET" to transfer larger amounts of data.

For important information on domain-based security restrictions that apply to loadVariables( ), see System.security.

Usage

The results of multiple loadVariable( ) calls to the same url may be cached by some browsers, preventing new data from loading from the server. To avoid this problem, append a dummy variable to each loadVariables( ) call so that the URL is unique. For example, here we generate a unique URL by appending the time in milliseconds:

loadVariables("http://www.mysite.com/cgi-bin/myScript.pl?cacheKiller=" 
              + new Date().getTime(), serverResponse);

Bugs

The POST method is not supported by Internet Explorer 4.5 on Macintosh. This problem was fixed in Version 5 of the browser.

On Macintosh, the loadVariables( ) method does not work with Flash 5 executable Projectors (i.e., movies bundled with the Flash 5 Standalone Player into a single executable file). However, loadVariables( ) does work with movies opened manually in the Standalone Player. To work around this problem, send data via the XML.send( ) and XML.sendAndLoad( ) methods.

See Also

loadVariablesNum( ), the LoadVars class, MovieClip.loadVariables( ), System.security.allowDomain( ); Chapter 17


Table of Contents