Created
January 17, 2024 15:14
-
-
Save esedic/8c4491460779730e4a2300a968a9dd4f to your computer and use it in GitHub Desktop.
Remove URL parameters and reload page
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
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(); | |
} | |
} |
Thank you!
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
Thanks for sharing this! It works well!