Last active
December 17, 2015 05:59
-
-
Save catdad/5562288 to your computer and use it in GitHub Desktop.
parse the query string of a URL and return an object
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 parseQuery(){ | |
var query = {}; | |
var temp = window.location.search.substring(1).split('&'); | |
for (var i = temp.length; i--;) { | |
var q = temp[i].split('='); | |
query[q.shift()] = q.join('='); | |
} | |
return query; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This function will not unescape strings. To do so, use this line:
query[q.shift()] = unescape(q.join('='));
To also unescape query keys, use this line:
query[unescape(q.shift())] = unescape(q.join('='));
Unescaped keys with spaces will not be accessible through the dot operator --
query.thing
-- and will instead need to be accessed by index --query["multiword thing"]
.