The Power of LoadVars Object - Posted on Aug 06, 2003 - Last
updated on June 24, 2004
The LoadVars object is an alternative to the loadVariables action for transferring variables between a Flash movie and a server.
You can use the LoadVars object to obtain verification of
successful data loading, progress indications, and stream data while it
downloads. The LoadVars object works much like the XML object; it uses the
methods load , send , and sendAndLoad to communicate with a server. The main
difference between the LoadVars object and the XML object is that LoadVars
transfers ActionScript name and value pairs, rather than an XML DOM tree stored
in the XML object.
The LoadVars object is supported by Flash Player 6 and
later.
Example 1:
Sending and loading variables from an ASP/ASP.NET/PHP page
/* Declare and initialize two LoadVars objects for sending and receiving variables */
var reply_lv = new LoadVars(); var send_lv = new LoadVars();
/* Define and associate the variables to LoadVars object that are required to send to
ASP/ASP.NET/PHP page
page */
send_lv.type = "BUTTON"; send_lv.id = "1";
/* Use sendAndLoad method to send the above two variables to ASP/ASP.NET/PHP page
as POST variables and assign the received variables to reply_lv LoadVars object */
send_lv.sendAndLoad("asp_net_php_page", reply_lv, "POST");
/* Use onLoad event handler method to assign a function which fires when the
variables from ASP/ASP.NET/PHP page
are received */
reply_lv.onLoad = loadedDotNetVars;
/* Function for handling the variables received fron the ASP/ASP.NET/PHP page
*/
function loadedDotNetVars(){ /* perform some actions with the received variables... you can access the variables using this.variablename */ }
Note: Unlike loadVariables, LoadVars object will send only the associated variables to the server.
Note: You can access the flash variables in ASP or ASP.NET using Request
object like Request("postvariable") and you can also pass variables from ASP or ASP.NET using
Response Object like Response.Write("&variablename=data"). You can pass as many variables as you require by seperating them with ampersand (&)
symbol. In the similar way you can use $_POST['postvariable'] to access flash
variables and you can use print or echo
language constructs to send data to flash.
Example 2:
Using LoadVars Object to load variables from a text file
var load_lv = new LoadVars() load_lv.load("textfile.txt"); load_lv.onLoad = loadTextVariables;
function loadTextVariables(){ /* you can perform actions on the received variables... you can access the variables using this.variablename */ }