Skip to content

Instantly share code, notes, and snippets.

@adamculpepper
Last active December 18, 2019 22:20
Show Gist options
  • Save adamculpepper/84c7a4027782be789d861cde069d4dc1 to your computer and use it in GitHub Desktop.
Save adamculpepper/84c7a4027782be789d861cde069d4dc1 to your computer and use it in GitHub Desktop.
JavaScript: Get URL Parameters (simple)
//source: https://stackoverflow.com/a/5158301
function getUrlParam(name) {
var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.href);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
//USAGE
//assumed url: http://example.com?page=3&sort=asc&perPage=20
getUrlParam('page');
getUrlParam('sort');
getUrlParam('perPage');
function getParam(param){
return new URLSearchParams(window.location.search).get(param);
}
getUrlParam('location');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment