About variables 105
This code passes a single variable called myURL, which contains the string
http://weblogs.macromedia.com. When the SWF file loads, a property named myURL
is created in the _level0 scope. One of the advantages of using FlashVars or passing
variables along the URL is that the variables are immediately available in Flash when the
SWF file loads. This means you don’t have to write any functions to check if the variables
have finished loading, which you would need to do if you loaded variables using LoadVars
or XML.
7. Save your changes to the HTML document, and then close it.
8. Double click myflashvars.html to test the application.
The text
http://weblogs.macromedia.com, a variable in the HTML file, appears in the
SWF file.
Loading variables from a server
There are several ways to load variables into Flash from external sources (such as text files,
XML documents, and so on). You can find much more information on loading variables,
including name/value pairs, in Chapter 16, “Working with External Data,” on page 633.
In Flash, you can easily load variables using the LoadVars class, as shown in the next example.
To load variables from a server:
1. Create a new Flash document.
2. Select Frame 1 of the Timeline, and add the following ActionScript in the Actions panel:
var my_lv:LoadVars = new LoadVars();
my_lv.onLoad = function(success:Boolean):Void {
if (success) {
trace(this.dayNames); // Sunday,Monday,Tuesday,...
} else {
trace("Error");
}
}
my_lv.load("http://www.helpexamples.com/flash/params.txt");
This code loads a text file from a remote server and parses its name/value pairs.
NOTE
All browsers will support string sizes as large as 64K (65,535 bytes) in length.
FlashVars must be assigned in both the object and embed tags in order to work on
all browsers.
TIP
Download or view the text file (http://www.helpexamples.com/flash/params.txt) in a
browser if you want to know how the variables are formatted.