Last active
September 1, 2016 12:43
-
-
Save larchanka/654974ab39d97ccfecce8a3102ea2ec7 to your computer and use it in GitHub Desktop.
Get query parameter from the url
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
var getQueryParam = function getQueryParam (param) { | |
var queries = window.location.search, regex, resRegex, results, response; | |
param = encodeURIComponent(param); | |
regex = new RegExp('[\\?&]' + param + '=([^&#]*)', 'g'); | |
resRegex = new RegExp('(.*)=([^&#]*)'); | |
results = queries.match(regex); | |
if (!results) { | |
return ''; | |
} | |
response = results.map(function (result) { | |
var parsed = resRegex.exec(result); | |
if (!parsed) { | |
return ''; | |
} | |
return parsed[2].match(/(^\d+$)/) ? Number(parsed[2]) : parsed[2]; | |
}) | |
return response.length > 1 ? response : response[0]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment