Macromedia Flex Macromedia Flex
Working with Querystrings
  Home

Feb 19, 2008 - Working with Querystrings
Using url embedded querystrings to leverage in your application

The first thing to do is to collect the querystrings and parse them into an array:

_all = ExternalInterface.call("window.location.href.toString");
_queryString = ExternalInterface.call("window.location.search.substring", 1);
if(_queryString) {
var params:Array = _queryString.split('&');
var length:uint = params.length;

for (var i:uint=0,index:int=-1; i var kvPair:String = params[i];
if((index = kvPair.indexOf("=")) > 0) {
var key:String = kvPair.substring(0,index);
var value:String = kvPair.substring(index+1);
_params[key] = value;
}
}
}


One of the issues you might run into is non-friendly ASCII characters, use the following function to strip them out:

public function convert(src:String):String {
if (src) {
var i:Number = src.indexOf("%25");
if (-1 != i) {
src = src.substr(0,i) + "[Yz" + src.substr(i+3);
}
while(true) {
i = src.indexOf("%");
if (i==-1)
break;
var n:Number = parseInt("0x"+src.substr(i+1,2));
src = src.substr(0,i) + String.fromCharCode(n) + src.substr(i+3);
}
i = src.indexOf("[Yz");
if (-1 != i) {
src = src.substr(0,i) + "%" + src.substr(i+3);
}
return src;
}
return null;
}



Billy Blackerby
bblackerby@tci-usa.com
http://www.tci-usa.com

File Details
Created On Feb, 19, 2008 by Billy Blackerby
Last Modified On Feb, 19, 2008 by Billy Blackerby
Group: Tips and Articles
Flex Versions: 2.0
Category: General
Type: Tip
Difficulty: Intermediate
Keywords: Querystring, ExternalInterface, URL