Visit stackoverflow.com/#_=_
and window.location.hash
evaluates to #_=_
. Fine.
Now execute window.location.hash = ''
to clear the hash, and the URL becomes stackoverflow.com/#
. (Notice the trailing #
.)
Why is the #
in window.location.hash
inconsistently included or excluded? How can the #
be removed from the URL without reloading the page?
(MDN says
[the hash is] the part of the URL that follows the # symbol, including the # symbol.
but that is not true for in the case of an empty hash.)
stackoverflow.com/#
also results inwindow.location.hash === ''
so it's consistent behavior. – Bertinehash
with JavaScript leads to#
in URL and emptyhash
property. Now visiting page with only#
in URL also leads to emptyhash
property.hash
property is filled only when there are other characters after#
in URL and it is consistent behavior in all browsers. – Bertine