Last active
February 22, 2021 10:23
-
-
Save sureshHARDIYA/534cbbce9e081456f05fbacdac084889 to your computer and use it in GitHub Desktop.
Check if a URL has query string
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
/** | |
* 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