How to remove hash from URL with JavaScript

January 24, 2021

You usually clear the URL hash like this:

location.hash = ""

However, that leaves a # at the end of the URL. You can remove the # by adding a line of code:

location.hash = ""
history.replaceState("", "", location.pathname)

The second line overwrites the URL with the current URL, minus the #.

A quick favor: was anything I wrote incorrect or misspelled, or do you still have questions? Please use this form to let me know or ask for help!