Return to Snippet

Revision: 36740
at November 25, 2010 15:34 by mhulse


Updated Code
package
{
	import flash.external.*;
 
	public class QueryString
	{
		private var _queryString:String;
		private var _all:String;
		private var _params:Object;
 
		public function QueryString(url:String='')
		{
			readQueryString(url);
		}
		public function get getQueryString():String
		{
			return _queryString;
		}
		public function get url():String
		{
			return _all;
		}
		public function get parameters():Object
		{
			return _params;
		}		
 
		private function readQueryString(url:String=''):void
		{
			_params = new Object();
			try
			{
				_all = (url.length > 0) ? url : ExternalInterface.call("window.location.href.toString");
				_queryString = (url.length > 0) ? url.split("?")[1] : ExternalInterface.call("window.location.search.substring", 1);
				if(_queryString)
				{
					var allParams:Array = _queryString.split('&');
					//var length:uint = params.length;
 
					for(var i:int=0, index=-1; i < allParams.length; i++)
					{
						var keyValuePair:String = allParams[i];
						if((index = keyValuePair.indexOf("=")) > 0)
						{
							var paramKey:String = keyValuePair.substring(0,index);
							var paramValue:String = keyValuePair.substring(index+1);
							_params[paramKey] = paramValue;
						}
					}
				}
			}
			catch(e:Error)
			{
				trace("Some error occured. ExternalInterface doesn't work in Standalone player.");
			}
		}
	}
}

//
// Call it like so:
//
var myPath:QueryString = new QueryString("http://www.studiosedition.com/?page=articles");
trace(myPath.parameters.page);

Revision: 36739
at November 25, 2010 15:33 by mhulse


Initial Code
package
{
	import flash.external.*;
 
	public class QueryString
	{
		private var _queryString:String;
		private var _all:String;
		private var _params:Object;
 
		public function QueryString(url:String='')
		{
			readQueryString(url);
		}
		public function get getQueryString():String
		{
			return _queryString;
		}
		public function get url():String
		{
			return _all;
		}
		public function get parameters():Object
		{
			return _params;
		}		
 
		private function readQueryString(url:String=''):void
		{
			_params = new Object();
			try
			{
				_all = (url.length > 0) ? url : ExternalInterface.call("window.location.href.toString");
				_queryString = (url.length > 0) ? url.split("?")[1] : ExternalInterface.call("window.location.search.substring", 1);
				if(_queryString)
				{
					var allParams:Array = _queryString.split('&');
					//var length:uint = params.length;
 
					for(var i:int=0, index=-1; i < allParams.length; i++)
					{
						var keyValuePair:String = allParams[i];
						if((index = keyValuePair.indexOf("=")) > 0)
						{
							var paramKey:String = keyValuePair.substring(0,index);
							var paramValue:String = keyValuePair.substring(index+1);
							_params[paramKey] = paramValue;
						}
					}
				}
			}
			catch(e:Error)
			{
				trace("Some error occured. ExternalInterface doesn't work in Standalone player.");
			}
		}
	}
}

Initial URL
http://blog.studiosedition.com/2010/03/query-string-as3/

Initial Description

                                

Initial Title
How to access query string arguments with AS3

Initial Tags
url, query

Initial Language
ActionScript 3