Created
March 20, 2013 11:25
-
-
Save adamcbrewer/5203966 to your computer and use it in GitHub Desktop.
JS: Query string to object
This file contains 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
/** | |
* Converts a URL query string to a javascript object | |
* | |
* @author Adam Brewer - @adamcbrewer - adamcbrewer.com | |
* | |
* Usage: "?test=true&something=false".queryToObj(?); | |
* | |
* Output: {test: true, something: false} | |
* | |
*/ | |
String.prototype.queryToObj = function (strip) { | |
strip = strip || '?'; | |
var string = this.replace(strip, ''), | |
obj = {}, | |
queryParams = string.split('&'), | |
i = 0, | |
l = queryParams.length; | |
for (i; i < l; i++) { | |
var params = queryParams[i].split('='), | |
k = params[0], | |
v = params[1]; | |
obj[k] = v; | |
} | |
return obj; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment