Skip to content

Instantly share code, notes, and snippets.

@klinkby
Created February 23, 2016 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klinkby/b86b6049ca23f53f83fa to your computer and use it in GitHub Desktop.
Save klinkby/b86b6049ca23f53f83fa to your computer and use it in GitHub Desktop.
Deserialize and serialize a query string
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