清除URL哈希 [英] Clearing URL hash

查看:127
本文介绍了清除URL哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

访问 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆