Created
March 29, 2011 14:09
-
-
Save mitio/892421 to your computer and use it in GitHub Desktop.
A simple function to return a hash with parameters, parsed from a query string.
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
// Returns a hash with the query parameters, parsed from the query string given | |
// (or the current location's query string, if no arguments given) | |
// Based on code found here: | |
// http://stackoverflow.com/questions/901115/get-querystring-values-in-javascript/2880929#2880929 | |
function getQueryParams(queryString) { | |
var queryParams = {}, | |
e, | |
r = /([^&=]+)=?([^&]*)/g, | |
d = function (s) { return decodeURIComponent(s.replace(/\+/g, ' ')); }, | |
q = queryString || window.location.search.substring(1); | |
while (e = r.exec(q)) { | |
queryParams[d(e[1])] = d(e[2]); | |
} | |
return queryParams; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using regex for this is just wrong and will fail in some scenarios.
You'd better off using a tiny reusable module like:
https://github.com/sindresorhus/query-string