View Single Post
Old 09-05-2012, 11:43 AM   PM User | #5
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,455
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
code
Code:
// turn URL'QS into an object using a parser. takes full urls...
function parseQS(str) {

	function cast(v){var builtIn=cast.lut[v];return Number(v)||(builtIn!==undefined?builtIn:v);}; 
		cast.lut=({ 'true':true, 'false':false,"":null});
		
		
    var ob = {}, float = "",
        key = "",
        dc = decodeURIComponent;

    for (var i = 0, mx = str.length; i < mx; i++) {
        var it = str[i];
        if (it === "=") {
            key = float;
            float = "";
            continue;
        }
        if (!it.search(/^[?&]/)) {
            if (it === "&" && str.slice(i + 1, i + 5) === "amp;") {
                i = (i + 4);
                float += "&";
                continue;
            }
            if (key) {
                ob[key] = cast(dc(float));
            }
            key = "";
            float = "";
            continue;
        }
        float += it;
    }
    ob[key] = dc(float);
    return ob;
}
demo
Code:
alert(
 JSON.stringify(
  parseQS(
     "https://www.google.com/search?num=123&bool=false&q=css+cookies+switch+stylesheet&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-beta&channel=fflb"
  )
  , null, "\t"
 )
)

shows:
Code:
{
	"num": 123,
	"bool": false,
	"q": "css+cookies+switch+stylesheet",
	"ie": "utf-8",
	"oe": "utf-8",
	"aq": "t",
	"rls": "org.mozilla:en-US:official",
	"client": "firefox-beta",
	"channel": "fflb"
}
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote