Skip to content

Instantly share code, notes, and snippets.

@NathanGRomano
Created April 20, 2011 15:23
Show Gist options
  • Save NathanGRomano/931628 to your computer and use it in GitHub Desktop.
Save NathanGRomano/931628 to your computer and use it in GitHub Desktop.
A simple javascript object to read and manipulate the query string
var queryString = function(queryStr) {
this._query = [];
queryStr = queryStr || window.location.search.substr(1);
var pairs = queryStr.split("&");
for(var i in pairs) {
var keyval = pairs[i].split("=");
this._query[keyval[0]] = keyval[1];
}
return this;
};
queryString.prototype.get = function(key) {
return this._query[key];
};
queryString.prototype.set = function(key, value) {
this._query[key] = value;
return this;
};
queryString.prototype.getURL = function() {
var str = [];
for(var i in this._query) {
str.push(i + '=' + this._query[i]);
}
return window.location.href.split('?')[0] + '?' + str.join('&');
};
queryString.prototype.navigate = function(without_history) {
if(without_history) {
window.location.replace(this.getURL());
} else {
window.location.href = this.getURL();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment