Skip to content

Instantly share code, notes, and snippets.

@garcon
Created October 6, 2010 21:17
Show Gist options
  • Save garcon/614105 to your computer and use it in GitHub Desktop.
Save garcon/614105 to your computer and use it in GitHub Desktop.
Javascript Query String Parser
function ptq(q)
{
/* parse the query */
/* semicolons are nonstandard but we accept them */
var x = q.replace(/;/g, '&').split('&'), i, name, t;
/* q changes from string version of query to object */
for (q={}, i=0; i<x.length; i++)
{
t = x[i].split('=', 2);
name = unescape(t[0]);
if (!q[name])
q[name] = [];
if (t.length > 1)
{
q[name][q[name].length] = unescape(t[1]);
}
/* next two lines are nonstandard */
else
q[name][q[name].length] = true;
}
return q;
}
function param() {
return ptq(location.search.substring(1).replace(/\+/g, ' '));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment