Created
February 2, 2018 07:51
-
-
Save jeroenbourgois/77bd5cabef1586c8bced2d413688398e to your computer and use it in GitHub Desktop.
parse a querystring in javascript
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
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