Last active
December 18, 2019 22:20
-
-
Save adamculpepper/84c7a4027782be789d861cde069d4dc1 to your computer and use it in GitHub Desktop.
JavaScript: Get URL Parameters (simple)
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
//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'); |
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
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