Skip to content

Instantly share code, notes, and snippets.

@sureshHARDIYA
Last active February 22, 2021 10:23
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 sureshHARDIYA/534cbbce9e081456f05fbacdac084889 to your computer and use it in GitHub Desktop.
Save sureshHARDIYA/534cbbce9e081456f05fbacdac084889 to your computer and use it in GitHub Desktop.
Check if a URL has query string
/**
* Return value if the current location has a query string.
*
* @params {string} lookUpString - The query string to search for.
* @returns {string} value of the query string if present.
*
* Example: getValueIfHasQueryString("v") <= "2"
**/
function getValueIfHasQueryString(lookUpString) {
const url = new URL(window.location.href);
const searchParams = new URLSearchParams(url.search);
return searchParams.get(lookUpString)
}
/**
* Return true/false if the current location has a query string.
*
* @params {string} lookUpString - The query string to search for.
* @returns {boolean} true/false of the query string if present.
*
* Example: getValueIfHasQueryString("v") <= true
**/
function hasQueryString(lookUpString) {
const url = new URL(window.location.href);
const searchParams = new URLSearchParams(url.search);
return searchParams.has(lookUpString);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment