Ultrashock Tutorials > Flash MX > FlashVars  
 
by: Gregory Burch, http://radio.weblogs.com/0107886


Source File

 
Using 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:

Object:
<PARAM NAME=FlashVars value=”foo=bar”>

Embed:
<EMBED SRC=”someswf.swf” FlashVars=”foo=bar”>

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:

Object:
<PARAM NAME=FlashVars value=”foo=bar&program=flash&language=en%2Dus”>

Embed:
<EMBED SRC=”someswf.swf” FlashVars=”foo=bar&program=flash&language=en%2Dus”>

_level0 will now contain three variables.

foo = bar
program = flash
language = en-us

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.

Boolean
Pass in a 0 for false and a 1 for true, do NOT pass in true or false. To convert the 0 or 1 to a Boolean use the Boolean function. An example is below:

/*
Variables passed from FlashVars are bFalse and bTrue:
bTrue = “1”
bFalse = “0”
*/

bTrue = Boolean(bTrue);
bFalse =Boolean(bFalse);

trace(bTrue); //true
trace(bFalse); /false

Number
This is an easy one; just use the Number function to convert it, as shown below:

myNumber = Number(myNumber);

Date
For date conversions it is best to use epoch time. Epoch time is defined as:

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
var dt = new Date();
trace(dt.getTime());

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:

<cfscript>
dateObj = createObject("java", "java.util.Date");
epoch = dateObj.getTime();
writeoutput( epoch.toString() );
</cfscript>

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.


Array
The easiest way to convert a variable from FlashVars into an Array is by using a delimiter and using the split method of String. Typically commas are used in this case. Below is an example of using the split method.

//this would come from FlashVars
//myArray = “1,2,3,4,5,6”;
myArray = myArray.split(",");

trace(myArray.length); //outputs 6

Object
Just like arrays, objects will be parsed by using delimiters. You now have to have two delimiters. You need a delimiter between the key and value and a delimiter separating key value pairs.

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 = "key|value,anotherKey|anotherValue,foo|bar";

myObject = myObject.split(",");
tmpObject = {};

for(var i = 0; i < myObject.length; i++){
   var tmp = myObject[i].split("|");
   tmpObject[tmp[0]] = tmp[1];
}

delete myObject;
myObject = tmpObject;

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.

Examples

  • BasicVars.fla & BasicVars.html – Example showing how to pass a variable through FlashVars.
  • CharTable.fla – A dynamically created conversion table.
  • Conversions.fla & Conversions.html – Example showing how to convert to common data types from FlashVar variables.
  • MultipleVars.fla & MultipleVars.fla – Example showing how to pass in multiple variables through FlashVars
 
©2002 Ultrashock.com Inc. - All rights reserved