Created
October 2, 2020 13:02
-
-
Save dkarmalita/c928d64f7164b0c95e6a9fd91bb5d427 to your computer and use it in GitHub Desktop.
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
export default function getSearchValueFromUrl(searchKey, removeFromUrl) { | |
const { origin, pathname, search, hash } = window.location; | |
const params = new URLSearchParams(search); | |
const value = params.get(searchKey); | |
if (removeFromUrl) { | |
params.delete(searchKey); | |
const nextsearch = params.toString(); | |
const newHref = `${origin}${pathname}${nextsearch ? `?${nextsearch}` : ''}${hash}`; | |
window.history.replaceState(null, '', newHref); | |
} | |
return value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment