如何使用 JavaScript 从 window.location (URL) 中删除哈希而不刷新页面?
我的 URL 如下:http://example.com#something
,如何删除 #something
,而不导致页面刷新?
我尝试了以下解决方案:
window.location.hash = '';
但是,这不会从 URL 中删除哈希符号 #
。
I have URL like: http://example.com#something
, how do I remove #something
, without causing the page to refresh?
I attempted the following solution:
window.location.hash = '';
However, this doesn't remove the hash symbol #
from the URL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
这也将删除尾随哈希。
例如: http://test.com/123#abc -> http://test.com/123
This will remove the trailing hash as well.
eg: http://test.com/123#abc -> http://test.com/123
要删除哈希,您可以尝试使用此函数
To remove the hash, you may try using this function
下面的怎么样?
window.location.hash=' '
请注意,我将哈希设置为单个空格而不是空字符串。
将哈希设置为无效锚点也不会导致刷新。例如,
window.location.hash='invalidtag'
但是,我发现上述解决方案具有误导性。这似乎表明在给定位置上有一个具有给定名称的锚点,尽管实际上没有。同时,使用空字符串会导致页面移动到顶部,这有时是不可接受的。使用空格还可以确保每当复制 URL、添加书签并再次访问该 URL 时,该页面通常会位于顶部并且空格将被忽略。
而且,嘿,这是我在 StackOverflow 上的第一个回答。希望有人觉得它有用并且符合社区标准。
How about the following?
window.location.hash=' '
Please note that am setting the hash to a single space and not an empty string.
Setting the hash to an invalid anchor does not cause a refresh either. Such as,
window.location.hash='invalidtag'
But, I find above solution to be misleading. This seems to indicate that there is an anchor on the given position with the given name although there isn't one. At the same time, using an empty string causes page to move to the top which can be unacceptable at times. Using a space also ensures that whenever the URL is copied and bookmarked and visited again, the page will usually be at the top and the space will be ignored.
And, hey, this is my first answer on StackOverflow. Hope someone finds it useful and it matches the community standards.
将此代码放在头部
put this code on head section
我想,这样会更安全
I think, it would be more safe
请尝试以下操作:
Try the following:
这是使用 href 更改位置并清除散列而不滚动的另一种解决方案。
此处解释了神奇的解决方案。规格此处。
注意:提议的 API 现在是 WhatWG HTML Living Standard 的一部分
Here is another solution to change the location using href and clear the hash without scrolling.
The magic solution is explained here. Specs here.
NOTE: The proposed API is now part of WhatWG HTML Living Standard
根据上面给出的答案之一,使用:
Building off one of the answers given above, use this:
您可以将 hash 替换为 null
You can replace hash with null
如今解决这个问题更加触手可及。 HTML5 History API 允许我们操作地址栏以显示其中的任何 URL当前域。
工作演示:http://jsfiddle.net/AndyE/ycmPt/show/
这个适用于 Chrome 9、Firefox 4、Safari 5、Opera 11.50 和 IE 10。对于不受支持的浏览器,您始终可以编写一个优雅的降级脚本,在可用的情况下使用它:
这样您就可以摆脱哈希符号,只是还没有在所有浏览器中出现。
注意:如果要替换浏览器历史记录中的当前页面,请使用
replaceState()
而不是pushState()
。Solving this problem is much more within reach nowadays. The HTML5 History API allows us to manipulate the location bar to display any URL within the current domain.
Working demo: http://jsfiddle.net/AndyE/ycmPt/show/
This works in Chrome 9, Firefox 4, Safari 5, Opera 11.50 and in IE 10. For unsupported browsers, you could always write a gracefully degrading script that makes use of it where available:
So you can get rid of the hash symbol, just not in all browsers — yet.
Note: if you want to replace the current page in the browser history, use
replaceState()
instead ofpushState()
.初始问题:
或者
两者都会返回不带哈希值或后面任何内容的 URL。
关于您的编辑:
对
window.location
的任何更改都将触发页面刷新。您可以更改window.location.hash
而不触发刷新(尽管如果您的哈希与页面上的 ID 匹配,窗口将会跳转),但您无法摆脱哈希符号。选择哪个更糟糕...最新答案
关于如何在不做出牺牲(完全重新加载或将哈希符号留在那里)的情况下做到这一点的正确答案是在这里。尽管将这个答案留在这里,但它是 2009 年的原始答案,而利用新浏览器 API 的正确答案是在 1.5 年后给出的。
Initial question:
or
both will return the URL without the hash or anything after it.
With regards to your edit:
Any change to
window.location
will trigger a page refresh. You can changewindow.location.hash
without triggering the refresh (though the window will jump if your hash matches an id on the page), but you can't get rid of the hash sign. Take your pick for which is worse...MOST UP-TO-DATE ANSWER
The right answer on how to do it without sacrificing (either full reload or leaving the hash sign there) is up here. Leaving this answer here though with respect to being the original one in 2009 whereas the correct one which leverages new browser APIs was given 1.5 years later.
(太多的答案都是多余且过时的。)现在最好的解决方案是这样的:
(Too many answers are redundant and outdated.) The best solution now is this:
简洁优雅:
Simple and elegant:
您可以按如下方式进行:
You can do it as below: