Skip to content

Instantly share code, notes, and snippets.

@catdad
Last active December 17, 2015 05:59
Show Gist options
  • Save catdad/5562288 to your computer and use it in GitHub Desktop.
Save catdad/5562288 to your computer and use it in GitHub Desktop.
parse the query string of a URL and return an object
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;
}
@catdad
Copy link
Author

catdad commented Sep 30, 2013

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"].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment