Skip to content

Instantly share code, notes, and snippets.

@aghouseh
Last active December 16, 2015 19:30
Show Gist options
  • Save aghouseh/5486012 to your computer and use it in GitHub Desktop.
Save aghouseh/5486012 to your computer and use it in GitHub Desktop.
string prototype to get all query values from a URL string or alternatively search for a specific value. pieced together from various things i found on the googles.
String.prototype.getParameters = function(key){
var urlParams,
match,
pl = /\+/g, // Regex for replacing addition symbol with a space
search = /([^&=]+)=?([^&]*)/g,
decode = function (s) { return decodeURIComponent(s.replace(pl, ' ')); },
query = this;
// this is hasty and fragile currently
if (query.indexOf('?') !== -1) {
query = query.split('?').slice(1).join('?');
}
urlParams = {};
while (match = search.exec(query)) {
urlParams[decode(match[1])] = decode(match[2]);
}
if (key) {
return (typeof urlParams[key]) ? urlParams[key] : false;
} else {
return urlParams;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment