I recently had a flash project that required me to get query string variables and found it cumbersome, however i found a query string class here on the forums originally created by MichaelxxOA over at actionscript.org. http://www.actionscript.org/forums/s…31&postcount=2
I reqrote it and structured it to act like Request.QueryString() familiar to those that have worked with asp and aspx.
I also added some functionality to datatype the variables as well. For example, and numeric variable value will become a number once it is pulled in and a true or false will be converted to Boolean.
Anyway, I figured I would share…
//External Interface Class
import flash.external.ExternalInterface;
class Request
{
//STATIC PROPERTIES
//----------------------------------------------
//Switch to Load Vars if they haven't already been loaded
private static var hasLoaded:Boolean = false;
//Define Javascript Function
private static var getURLFunction:String = "function get_url(){return window.location.toString();}";
//Array to store values once they have been loaded.
private static var values:Array = new Array();
//CONSTRUCTOR
//----------------------------------------------
public function Request(){}
//PUBLIC STATIC METHODS
//----------------------------------------------
//--Query String
public static function QueryString(varName:String)
{
//Pull Valuse From Query String if you havent already.
if(!hasLoaded)
{
hasLoaded = true;
getValueArray();
}
return values[varName];
}
//--Get Values From Query String
private static function getValueArray():Void
{
trace("]> Loading Variables from Query String.");
var o:Array = new Array();
//Call Javascript Function
o = ExternalInterface.call(getURLFunction).split("?")[1].split("&");
for(var i:Number = 0; i < o.length; i ++)
{
var tempName:String = o*.split("=")[0];
var tempValue:String = o*.split("=")[1];
trace(" - " + tempName + " = " + tempValue);
if(!isNaN(Number(tempValue)))
{
//Datatype value as a Number
values[tempName] = Number(tempValue);
}else if(tempValue.toLowerCase() == "true" || tempValue.toLowerCase() == "false"){
//DataType value as a Boolean
values[tempName] = Boolean(tempValue);
}else{
values[tempName] = tempValue;
}
}
}
}
This is how you would use it
[FONT=Courier New]
var qsVar = Request.QueryString("varName");
[/FONT]