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"
}