Skip to content

Instantly share code, notes, and snippets.

@jeroenbourgois
Created February 2, 2018 07:51
Show Gist options
  • Save jeroenbourgois/77bd5cabef1586c8bced2d413688398e to your computer and use it in GitHub Desktop.
Save jeroenbourgois/77bd5cabef1586c8bced2d413688398e to your computer and use it in GitHub Desktop.
parse a querystring in javascript
function parseQueryString(queryString) {
queryString = queryString.substring(1)
var params = {},
queries,
temp,
i,
l
// Split into key/value pairs
queries = queryString.split('&')
// Convert the array of strings into an object
for (i = 0, l = queries.length; i < l; i++) {
temp = queries[i].split('=')
params[temp[0]] = temp[1]
}
return params
}
function parsedQueryStringToString(params) {
var arr = []
for (var key in params) {
if (params.hasOwnProperty(key)) {
arr.push(key + '=' + params[key])
}
}
return arr.join('&')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment