Created
September 23, 2013 22:12
-
-
Save leopic/6677677 to your computer and use it in GitHub Desktop.
helper to retrieve url params
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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