Skip to content

Instantly share code, notes, and snippets.

@larchanka
Last active September 1, 2016 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larchanka/654974ab39d97ccfecce8a3102ea2ec7 to your computer and use it in GitHub Desktop.
Save larchanka/654974ab39d97ccfecce8a3102ea2ec7 to your computer and use it in GitHub Desktop.
Get query parameter from the url
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