清除URL哈希 [英] Clearing URL hash
问题描述
stackoverflow.com /#_ = _
和 window.location.hash
的计算结果为#_ = _
。现在执行 window.location.hash =''
来清除哈希,并且URL变为 code> stackoverflow.com /#
。 (注意尾部#
。) 为什么#
在 window.location.hash
中不一致包含或排除?如何从网址中删除#
而无需重新加载页面? MDN 称:
[该散列是]#符号后面的URL部分,包括#符号。
但这不适用于in )
要回答第二个问题(删除#
没有页面刷新):
history.pushState('',document.title,window.location。路径名);
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.)
To answer the second question (removing the #
without a page refresh):
history.pushState('', document.title, window.location.pathname);
这篇关于清除URL哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!