396 Working with Text and Strings
Loading variables by using LoadVars
The LoadVars class also lets you load variables in a URL-encoded format, similar to passing
variables in the query string in a web browser. The following example demonstrates how to
load a remote text file into a SWF file and display its variables,
monthNames and dayNames.
To load variables from a text file by using LoadVars:
1. Create a new Flash document and save it as loadvarsVariables.fla.
2. Add the following code to Frame 1 of the Timeline:
this.createTextField("my_txt", 10, 10, 10, 320, 100);
my_txt.autoSize = "left";
my_txt.border = true;
my_txt.multiline = true;
my_txt.wordWrap = true;
var lorem_lv:LoadVars = new LoadVars();
lorem_lv.onLoad = function (success:Boolean):Void {
if (success) {
my_txt.text = "dayNames: " + lorem_lv.dayNames + "\n\n";
my_txt.text += "monthNames: " + lorem_lv.monthNames;
} else {
my_txt.text = "Unable to load external file.";
}
}
/* contents of params.txt:
&monthNames=January,February,...&dayNames=Sunday,Monday,...
*/
lorem_lv.load("http://www.helpexamples.com/flash/params.txt");
3.
Save the Flash document and select Control > Test Movie from the main menu.
Because you are using the
LoadVars.onLoad() method instead of LoadVars.onData(),
Flash parses out the variables and creates variables within the LoadVars object instance.
The external text file contains two variables,
monthNames and dayNames, which both
contain strings.
For information on security, see Chapter 17, “Understanding Security.”