Skip to content

Instantly share code, notes, and snippets.

@bunnymatic
Created November 17, 2012 22:16
Show Gist options
  • Save bunnymatic/4100687 to your computer and use it in GitHub Desktop.
Save bunnymatic/4100687 to your computer and use it in GitHub Desktop.
simple querystring parser in javascript using the DOM
var QueryStringParser;
QueryStringParser = (function() {
function QueryStringParser(url) {
var parser, _that;
this.query_params = {};
if (!document || !document.createElement) {
throw 'This needs to be run in an HTML context with a document.';
}
parser = document.createElement('a');
parser.href = url;
this.url = url;
if (parser.origin) {
this.origin = parser.origin;
} else {
this.origin = [parser.protocol, '//', parser.host].join('');
}
this.protocol = parser.protocol;
this.pathname = parser.pathname;
this.hash = parser.hash;
_that = this;
_.each(parser.search.substr(1).split('&'), function(params) {
var kv;
kv = params.split('=');
return _that.query_params[kv[0]] = kv[1];
});
}
QueryStringParser.prototype.toString = function() {
var bits, q;
q = _.compact(_.map(this.query_params, function(v, k) {
if ((typeof v !== 'undefined') && (v !== null)) {
return [k, v].join('=');
}
})).join('&');
bits = [this.origin, this.pathname].join('');
if (q) {
bits += "?" + q;
}
if (this.hash) {
bits += this.hash;
}
return bits;
};
return QueryStringParser;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment