Last active
November 11, 2015 17:25
-
-
Save jtribble/f2f5ec35a4b0a13d167b to your computer and use it in GitHub Desktop.
Get URL Parameter
This file contains hidden or 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
/** | |
* 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; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tested on latest versions of Chrome, Firefox, Safari, IE.