Skip to content

Instantly share code, notes, and snippets.

@sudosoul
Created February 27, 2019 23:26
Show Gist options
  • Save sudosoul/33fbf86c9dd2bfe48ff995ba02cb9068 to your computer and use it in GitHub Desktop.
Save sudosoul/33fbf86c9dd2bfe48ff995ba02cb9068 to your computer and use it in GitHub Desktop.
getUrlParams.js Usage Example
// 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