Created
February 27, 2019 23:26
-
-
Save sudosoul/33fbf86c9dd2bfe48ff995ba02cb9068 to your computer and use it in GitHub Desktop.
getUrlParams.js Usage Example
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
// Using location.search (gets querystring from current URL) | |
let urlParams = getUrlParams(location.search); // Assume location.search = "?a=1&b=2b%202" | |
console.log(urlParams); // Prints { "a": 1, "b": "2b 2" } | |
// Using a URL string | |
const url = 'https://example.com?a=A%20A&b=1'; | |
urlParams = getUrlParams(url); | |
console.log(urlParams); // Prints { "a": "A A", "b": 1 } | |
// To check if a parameter exists, simply do: | |
if (urlParams.hasOwnProperty('a')) { | |
console.log(urlParams.a); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment