Created
February 23, 2016 10:10
-
-
Save klinkby/b86b6049ca23f53f83fa to your computer and use it in GitHub Desktop.
Deserialize and serialize a query string
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
function QueryString(qs) { | |
var re = /([^&=]+)=([^&]*)/g, | |
m; | |
while (m = re.exec((qs || location.search).substring(1))) { // skip the question mark | |
this[decodeURIComponent(m[1])] = decodeURIComponent(m[2]); | |
} | |
} | |
QueryString.prototype.toString = function () { | |
var ser = "?"; | |
for (var key in this) { | |
if (!this.propertyIsEnumerable(key)) continue; | |
ser += encodeURIComponent(key) + "=" + encodeURIComponent(this[key]) + "&"; | |
} | |
return 1 === ser.length ? "" : ser.substring(0, ser.length - 1); // skip the trialing ampersand | |
}; | |
// example | |
// x = new QueryString("?q=daxxxv&ia=about"); | |
// x.toString() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment