Skip to content

Instantly share code, notes, and snippets.

@esedic
Created January 17, 2024 15:14
Show Gist options
  • Save esedic/8c4491460779730e4a2300a968a9dd4f to your computer and use it in GitHub Desktop.
Save esedic/8c4491460779730e4a2300a968a9dd4f to your computer and use it in GitHub Desktop.
Remove URL parameters and reload page
function removeParamAndRefresh(paramToRemove) {
const url = new URL(window.location.href);
if (url.searchParams.has(paramToRemove)) {
url.searchParams.delete(paramToRemove);
// Update the URL in the address bar without reloading
window.history.pushState({}, '', url);
// Now, reload the page
window.location.reload();
}
}
@johnjago
Copy link

Thanks for sharing this! It works well!

@devdiogenes
Copy link

Thank you!

@devdiogenes
Copy link

Instead of pushState and reload, we can also use only window.location.href = url.href, that will change url and reload by once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment