Skip to content

Instantly share code, notes, and snippets.

@leopic
Created September 23, 2013 22:12
Show Gist options
  • Save leopic/6677677 to your computer and use it in GitHub Desktop.
Save leopic/6677677 to your computer and use it in GitHub Desktop.
helper to retrieve url params
// Based off the work from https://github.com/mattpass
_.mixin({
inUrl: function(singleParam) {
var allParams = _.map(location.search.slice(1).split('&'), function(currentParam) {
return { 'Key': currentParam.split('=')[0],
'Value': decodeURIComponent(currentParam.split('=')[1]).replace(/\+/g,' '),
'Raw Value': currentParam.split('=')[1]
}
});
var paramKeys = _.pluck(allParams, 'Key');
if (singleParam) {
var idx = _.indexOf(paramKeys, singleParam, false);
if (idx > -1) {
return allParams[idx]['Value'];
} else {
return false;
}
} else {
return allParams;
}
}
});
_.inUrl(); // Retrieves all params as an Object Literal
_.inUrl('param'); // If the param is the URL arguments it will return the value of the param or false if it's not part of the params.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment