Last active
August 29, 2015 13:57
-
-
Save Manc/9409355 to your computer and use it in GitHub Desktop.
Parse Query String in JavaScript. Demo: http://jsfiddle.net/nicz/hyDKE/
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
/** | |
* 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