使浏览器在单击后退按钮时忽略URL哈希

Making browsers ignore the URL hash when the back button is clicked

本文关键字:URL 哈希 按钮 浏览器 单击      更新时间:2023-09-26

例如,如果用户在http://example.com,然后用户转到http://example.com#comments.如果用户在浏览器上点击"返回",我如何让他"忽略"http://example.com然后直接转到他之前访问过的URL?

我已经加载了jQuery。

而不是像这样的链接

<a href='#comments'>Link</a>

使用location.replace()"覆盖"浏览器历史记录中http://example.com的记录。

https://developer.mozilla.org/en/DOM/window.location

示例:

HTML:

<a id='commentsLink'>Link</a>

JavaScript:

$("#commentsLink").click(function(){
    window.location.replace("#comments");
});