|Ultrashock Tutorials > Flash MX > FlashVars|
What is the FlashVars
The FlashVars parameter allows you to pass in variables to a SWF in the Object and Embed tags in the html. FlashVars requires Flash Player 6 or later.
Passing a variable through FlashVars
The Object and Embed tags structured as shown below:
The code above will put a variable foo with value bar on the _level0 timeline. All variables passed in through FlashVars will be strings. You can do conversions in Flash at a later time but there is no way to pass in any other type through FlashVars. You can find more info about this in the section titled “Converting FlashVar types”
Passing in multiple variables through FlashVars
If you have used LoadVars in the past this will be very familiar to you. To pass in multiple variables all you need to do is separate them by using an ampersand (&). One example is:
_level0 will now contain three variables.
foo = bar
Notice that the language variable has en%2Dus instead of en-us. In Flash the value will be en-us but you must escape certain characters in the html. Read onto the next section for a better explanation.
The example files for this section are MultipleVars.fla and MultipleVars.html.
Escaping Characters in FlashVars
Escaping certain characters in FlashVars is a must. There are tons of tables on the Internet to tell you the URL-encoded format of special characters. The easiest way to figure out what to replace and what to replace it with, is by using the escape function in Flash. An example is below:
trace(escape(“This is some string that may need to be escaped, why? Because I use a lot of special characters such as &#^%&@)!@)$(%& but be careful to escape quotes within this such as \”, this stuff can get ugly and complicated can’t it….that’s life.”));
If you paste that into Flash and run it you will get the nice long ugly string that you should place in the FlashVars parameter. If you have long strings like that with a lot of special characters just use the escape function, it is much easier than replacing each item by hand. But of course if you would like to you can open the CharTable.fla and run it for an example URL-encoded conversion table.
Converting FlashVars' types
All the variables that are passed in through FlashVars are strings. Sometimes you may like to have other data types. There is no flawless, clean way to accomplish this. So most of the time you have to do conversions on your own. Follow the rules below when passing in certain types.
myNumber = Number(myNumber);
Milliseconds since midnight on January 1, 1970
Most languages use this as a conversion and comparison technique. To get the epoch time from a Date object you can call the getTime method.
//get the epoch of today
You can set the epoch by passing in the milliseconds into the setTime method.
To show that many different programming languages do support this here is a ColdFusion example that accesses the underlying java Date object:
Boolean, Number, and Date all have valid reasons to be passed in through FlashVars. Arrays and Objects however are a bit overboard. If you find yourself doing this you may want to re-think what you are doing. But for completeness you can find their examples below.
//this would come from FlashVars
myObject = "key|value,anotherKey|anotherValue,foo|bar";
To parse this you will have to utilize split again. Below is a code snippet that will parse the above code.
//this would come from FlashVars
myObject is now an object an contains the properties key, anotherKey, and foo
Conversions.fla and Conversions.html are example files that show the conversions to all these types.
©2002 Ultrashock.com Inc. - All rights reserved