Skip to content

Instantly share code, notes, and snippets.

@jtribble
Last active November 11, 2015 17:25
Show Gist options
  • Save jtribble/f2f5ec35a4b0a13d167b to your computer and use it in GitHub Desktop.
Save jtribble/f2f5ec35a4b0a13d167b to your computer and use it in GitHub Desktop.
Get URL Parameter
/**
* Get URL param
*
* @param {string} param
* @return {string|null}
*/
var getUrlParam = function (param) {
// if input is invalid or there are no query params, return null
if (typeof param !== 'string' || location.search === '') return null;
// otherwise, split the search string on &
var params = location.search.substring(1).split('&');
// loop through the params and see if we have a match
for (var i = 0; i < params.length; i++) {
var keyValuePair = params[i].split('=');
if (keyValuePair[0] === param) {
return decodeURI(keyValuePair[1]);
}
}
// if there's no match, return null
return null;
};
@jtribble
Copy link
Author

Tested on latest versions of Chrome, Firefox, Safari, IE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment