AS3: QueryString
View Documentation
Download Class
I've posted before on retrieving FlashVars and how to do it in AS3. FlashVars is the suggested way of grabbing in variables from outside of Flash, but if it isn't an option and you are still using old school query strings, this class will help you in grabbing variables out of the query string.
The class looks like this:
-
/**
-
* @author Matt Przybylski [http://www.reintroducing.com]
-
* @version 1.0
-
*/
-
-
package com.reintroducing.utils
-
{
-
import flash.external.ExternalInterface;
-
import flash.utils.Dictionary;
-
import flash.utils.getQualifiedClassName;
-
-
public class QueryString
-
{
-
//- PRIVATE & PROTECTED VARIABLES -------------------------------------------------------------------------
-
-
// singleton instance
-
private static var _instance:QueryString;
-
private static var _allowInstance:Boolean;
-
-
private var _pairDict:Dictionary;
-
private var _url:String;
-
private var _pairs:Array;
-
-
//- PUBLIC & INTERNAL VARIABLES ---------------------------------------------------------------------------
-
-
-
-
//- CONSTRUCTOR -------------------------------------------------------------------------------------------
-
-
// singleton instance of QueryString
-
public static function getInstance():QueryString
-
{
-
if (QueryString._instance == null)
-
{
-
QueryString._allowInstance = true;
-
QueryString._instance = new QueryString();
-
QueryString._allowInstance = false;
-
}
-
-
return QueryString._instance;
-
}
-
-
public function QueryString()
-
{
-
this.parseValues();
-
-
if (!QueryString._allowInstance)
-
{
-
throw new Error("Error: Use QueryString.getInstance() instead of the new keyword.");
-
}
-
}
-
-
//- PRIVATE & PROTECTED METHODS ---------------------------------------------------------------------------
-
-
private function parseValues():void
-
{
-
this._url = ExternalInterface.call("document.location.search.toString");
-
this._pairDict = new Dictionary(true);
-
this._pairs = this._url.split("?")[1].split("&");
-
-
for (var i:int = 0; i <this._pairs.length; i++)
-
{
-
var pairName:String = this._pairs[i].split("=")[0];
-
var pairValue:String = this._pairs[i].split("=")[1];
-
-
this._pairDict[pairName] = pairValue;
-
}
-
}
-
-
//- PUBLIC & INTERNAL METHODS -----------------------------------------------------------------------------
-
-
/**
-
* Returns the value of the specified query string parameter.
-
*
-
* @param $val A string identifying the parameter for whose value you want to retrieve
-
*
-
* @return String The value for the given parameter
-
*/
-
public function getValue($val:String):String
-
{
-
return this._pairDict[$val];
-
}
-
-
//- EVENT HANDLERS ----------------------------------------------------------------------------------------
-
-
-
-
//- GETTERS & SETTERS -------------------------------------------------------------------------------------
-
-
-
-
//- HELPERS -----------------------------------------------------------------------------------------------
-
-
public function toString():String
-
{
-
return getQualifiedClassName(this);
-
}
-
-
//- END CLASS ---------------------------------------------------------------------------------------------
-
}
-
}
It is a singleton and has a very simple usage. Consider the URL http://www.reintroducing.com?var1=one&var2=two. To retrieve var1, you would do the following:
-
import com.reintroducing.utils.QueryString;
-
-
var qs:QueryString = QueryString.getInstance();
-
-
trace(qs.getValue("var1"));
The values always come back as strings so you could treat them any way you like from there-on-out.











[...] http://evolve.reintroducing.com/2008/07/03/as3/as3-querystring/#more-141 [...]