Created
October 6, 2010 21:17
-
-
Save garcon/614105 to your computer and use it in GitHub Desktop.
Javascript Query String Parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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