Skip to content

Instantly share code, notes, and snippets.

@dkarmalita
Created October 2, 2020 13:02
Show Gist options
  • Save dkarmalita/c928d64f7164b0c95e6a9fd91bb5d427 to your computer and use it in GitHub Desktop.
Save dkarmalita/c928d64f7164b0c95e6a9fd91bb5d427 to your computer and use it in GitHub Desktop.
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