AS3: QueryString

View Documentation
Download Class

UPDATE 11/20/08: I've updated the class to fix the issue roi outlined in the comments. The download now goes the the new version, 1.1.

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:

Actionscript:
  1. package com.reintroducing.utils
  2. {
  3.     import flash.external.ExternalInterface;
  4.     import flash.utils.Dictionary;
  5.     import flash.utils.getQualifiedClassName;   
  6.    
  7.     /**
  8.      * Singleton used to grab data out of the query string.
  9.      *
  10.      * @author Matt Przybylski [http://www.reintroducing.com]
  11.      * @version 1.1
  12.      */
  13.     public class QueryString
  14.     {
  15. //- PRIVATE & PROTECTED VARIABLES -------------------------------------------------------------------------
  16.  
  17.         // singleton instance
  18.         private static var _instance:QueryString;
  19.         private static var _allowInstance:Boolean;
  20.        
  21.         private var _pairDict:Dictionary;
  22.         private var _url:String;
  23.         private var _pairs:Array;
  24.        
  25. //- PUBLIC & INTERNAL VARIABLES ---------------------------------------------------------------------------
  26.        
  27.        
  28.        
  29. //- CONSTRUCTOR -------------------------------------------------------------------------------------------
  30.    
  31.         // singleton instance of QueryString
  32.         public static function getInstance():QueryString
  33.         {
  34.             if (QueryString._instance == null)
  35.             {
  36.                 QueryString._allowInstance = true;
  37.                 QueryString._instance = new QueryString();
  38.                 QueryString._allowInstance = false;
  39.             }
  40.            
  41.             return QueryString._instance;
  42.         }
  43.        
  44.         public function QueryString()
  45.         {
  46.             this.parseValues();
  47.            
  48.             if (!QueryString._allowInstance)
  49.             {
  50.                 throw new Error("Error: Use QueryString.getInstance() instead of the new keyword.");
  51.             }
  52.         }
  53.        
  54. //- PRIVATE & PROTECTED METHODS ---------------------------------------------------------------------------
  55.        
  56.         private function parseValues():void
  57.         {
  58.             this._url = ExternalInterface.call("document.location.search.toString");
  59.             this._pairDict = new Dictionary(true);
  60.             this._pairs = this._url.split("?")[1].split("&");
  61.            
  62.             var pairName:String;
  63.             var pairValue:String;
  64.            
  65.             for (var i:int = 0; i <this._pairs.length; i++)
  66.             {
  67.                 pairName = this._pairs[i].split("=")[0];
  68.                 pairValue = this._pairs[i].split("=")[1];
  69.                
  70.                 this._pairDict[pairName] = pairValue;
  71.             }
  72.         }
  73.        
  74. //- PUBLIC & INTERNAL METHODS -----------------------------------------------------------------------------
  75.    
  76.         /**
  77.          * Returns the value of the specified query string parameter.
  78.          *
  79.          * @param $val A string identifying the parameter for whose value you want to retrieve
  80.          *
  81.          * @return String The value for the given parameter
  82.          */
  83.         public function getValue($val:String):String
  84.         {
  85.             if (this._pairDict[$val] == null)
  86.             {
  87.                 return "";
  88.             }
  89.             else
  90.             {
  91.                 return this._pairDict[$val];
  92.             }
  93.         }
  94.    
  95. //- EVENT HANDLERS ----------------------------------------------------------------------------------------
  96.    
  97.        
  98.    
  99. //- GETTERS & SETTERS -------------------------------------------------------------------------------------
  100.    
  101.        
  102.    
  103. //- HELPERS -----------------------------------------------------------------------------------------------
  104.    
  105.         public function toString():String
  106.         {
  107.             return getQualifiedClassName(this);
  108.         }
  109.    
  110. //- END CLASS ---------------------------------------------------------------------------------------------
  111.     }
  112. }

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:

Actionscript:
  1. import com.reintroducing.utils.QueryString;
  2.  
  3. var qs:QueryString = QueryString.getInstance();
  4.  
  5. trace(qs.getValue("var1"));

The values always come back as strings so you could treat them any way you like from there-on-out.

If you found this post useful, please consider leaving a comment, subscribing to the feed, or making a small donation.

66 Comments

Corban says that he error is occuring when tested inside of the IDE because there is no query string available until it is loaded on the net. to avoid this, you could use my Environment class (listed on this blog) to do a simple check of the isLive variable. I'd strongly suggest you take a look at that class and use it to test if the site is online or in the IDE, it has saved me a ton of headaches in the past year or so.and my apologies for taking so long to answer that,can anyone comment on this?

This doesn't work in Safari, specifically in the Facebook platform. Anyone else have the same issue?

Works fine in every other browser.

@neil: I have not worked much with the FB platform. What is the format at which the FB platform returns URLs in?

[...] QueryString     一个单例类,用来获取URL地址后所带参数值对http://evolve.reintroducing.com/2008/07/03/as3/as3-querystring/#more-1415、ActionScript 3 Contextual Menu Manager Class         [...]

posicionamiento web SEO | Posicionamiento web | Posicionamiento en buscadores: SEO y Posicionamiento Web buscadores. Consultoría de ... bit.ly/w1ZI2t 5:49 PM Dec 31st, 2011 ...scribe.twitter.com/Posicionate_Web/status/153291618659745793...

SEO | Posicionamiento web | Posicionamiento en buscadores: SEO y Posicionamiento Web buscadores. Consultoría de ... bit.ly/w1ZI2t 5:49 PM Dec 31st, 2011 ...scribe.twitter.com/Posicionate_Web/status/153291618659745793...

[...] 4、AS3: QueryString 一个单例类,用来获取URL地址后所带参数值对 http://evolve.reintroducing.com/2008/07/03/as3/as3-querystring/#more-141 [...]

[...] 4、AS3: QueryString     一个单例类,用来获取URL地址后所带参数值对 http://evolve.reintroducing.com/ … erystring/#more-141 [...]

[...] 4、AS3: QueryString     一个单例类,用来获取URL地址后所带参数值对 http://evolve.reintroducing.com/2008/07/03/as3/as3-querystring/#more-141 [...]

[...] 4、AS3: QueryString 一个单例类,用来获取URL地址后所带参数值对 http://evolve.reintroducing.com/2008/07/03/as3/as3-querystring/#more-141 5、ActionScript 3 Contextual Menu Manager Class AS3关联菜单管理类 [...]

[...] 4、AS3: QueryString     一个单例类,用来获取URL地址后所带参数值对http://evolve.reintroducing.com/2008/07/03/as3/as3-querystring/#more-141 [...]

[...] QueryString 一个单例类,用来获取URL地址后所带参数值对http://evolve.reintroducing.com/2008/07/03/as3/as3-querystring/#more-1415、ActionScript 3 Contextual Menu Manager Class [...]

[...] 4、AS3: QueryString     一个单例类,用来获取URL地址后所带参数值对 http://evolve.reintroducing.com/2008/07/03/as3/as3-querystring/#more-141 [...]

[...] 4、AS3: QueryString     一个单例类,用来获取URL地址后所带参数值对 http://evolve.reintroducing.com/2008/07/03/as3/as3-querystring/#more-141 [...]

Leave a comment

(required)

(required)