Skip to content

Instantly share code, notes, and snippets.

@Manc
Last active August 29, 2015 13:57
Show Gist options
  • Save Manc/9409355 to your computer and use it in GitHub Desktop.
Save Manc/9409355 to your computer and use it in GitHub Desktop.
Parse Query String in JavaScript. Demo: http://jsfiddle.net/nicz/hyDKE/
/**
* Convert a URL or just the query part of a URL to an
* object with all keys and values.
* Usage examples:
* // Get "GET" parameters from current request as object:
* var parameters = parseQueryString(window.location.search);
*/
function parseQueryString(query) {
var obj = {},
qPos = query.indexOf("?"),
tokens = query.substr(qPos + 1).split('&'),
i = tokens.length - 1;
if (qPos !== -1 || query.indexOf("=") !== -1) {
for (; i >= 0; i--) {
var s = tokens[i].split('=');
obj[unescape(s[0])] = s.hasOwnProperty(1) ? unescape(s[1]) : null;
};
}
return obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment