Table of Contents

LoadVars Class Flash 6

export variables to, or import variables from, an external source

Constructor

new LoadVars()

Properties

contentType

The MIME content type used for send( ) and sendAndLoad( ) operations.

loaded

Status of a load( ) or sendAndLoad( ) operation.

Methods

decode( )

Convert a URL-encoded string of variables to properties.

getBytesLoaded( )

Check what portion of loading variables has arrived, in bytes.

getBytesTotal( )

Check the expected total byte size of loading variables.

load( )

Import URL-encoded variables from a text file or a server-side application.

send( )

Transfer URL-encoded variables to a server-side application or script.

sendAndLoad( )

Transfer variables to a server-side application, and receive variables in return.

toString( )

Returns the URL-encoded string of variables that would be sent by send( ) or sendAndLoad( ).

Event Handlers

onData( )

Handler executed when external variable data finishes loading.

onLoad( )

Handler executed when external variables have been converted into object properties.

Description

The LoadVars class transfers variables to or from an external source, such as a text file or a server-side script. Table 18-9 shows typical LoadVars usage scenarios and lists the method associated with each scenario.

Table 18-9. LoadVars usage scenarios

Desired Action

Method

Retrieve external variables from a text file or server-side script.

LoadVars.load( )

Send ActionScript variables to a server-side script, and receive optional results in a web browser.

LoadVars.send( )

Send ActionScript variables to a server-side script, and receive results in Flash.

LoadVars.sendAndLoad( )

The send( ), load( ), and sendAndLoad( ) methods must be invoked through an instance of LoadVars, not through the class itself. The properties of the instance are either the variables to be sent or those that have been received. The send( ) method converts the properties of the LoadVars instance to a string of URL-encoded variables, and sends them as an HTTP request to a server-side application. The load( ) method retrieves URL-encoded variables and converts them to properties of the LoadVars object. The sendAndLoad( ) method does both. For details and examples, see each method's listing.

The LoadVars class provides the same basic service as the global loadVariables( ) function, first introduced in Flash 4, but in a more logical format and with several important advantages:

Variables transferred by a load( ) or sendAndLoad( ) operation are formatted according to the rules of URL encoding, as follows:

The following code shows the contents of a text file to be imported into Flash. The imported variables are name and address, which have the values "stephen" and "65 nowhere st!", respectively (note that the exclamation point is encoded as %21):

name=stephen&address=65+nowhere+st%21

Generally, server-side applications automatically URL encode variables for transmission to Flash, but variables in text files must be URL-encoded by hand. For example, in Flash Player 5 (or when System.useCodePage is true in Flash Player 6), the high-ASCII character é is at code point 233, which is hex E9, so it is written manually as %E9. The word resumé becomes resum%E9. In contrast, in Flash Player 6, when System.useCodePage is false (the default), the character é should be entered directly, and the variables file should be saved in Unicode UTF-8 format. (In Windows Notepad, the UTF-8 format can be chosen in the "Save as..." dialog box.) If the file cannot be saved as UTF-8, or if the character cannot be entered directly, the single-, double-, or triple-byte UTF-8 escape sequence for the character must be used. For example, the UTF-8 sequence for é is %C3%A9, so the word resumé would be written as resum%C3%A9. You can check the UTF-8 escape sequence for a character in Flash Player 6 using the escape( ) function. For example:

trace(escape("é"));  // Displays: %C3%A9

As a general rule, where many international characters are required, data should be loaded from Unicode-formatted XML files rather than via LoadVars.

For security reasons, LoadVars works only with hosts in the domain from which the movie was downloaded. For more information, see System.security.

The LoadVars class is not always the best means of importing data into Flash. For variables that need to be imported only once at load-time in a browser, consider using the FLASHVARS attribute of the HTML <OBJECT> and <EMBED> tags (see Section 2.6). For large quantities of data, consider using the XML class.

See Also

loadVariables( ), System.security, the XML class; "Loading External Variables," in Chapter 2; Chapter 17


Table of Contents