我在一个单页网站上工作,我想使用浏览器后退按钮返回指定的哈希,但我不希望用户能够使用前进按钮。我如何禁用它或弹出所有转发历史统计数据?
在下面的代码中,代码转发按钮不会引起任何问题,但我仍然想禁用它!
javascript:
var counter = 0;
var hashLocation = "page";
var selfChange = false;
function next() {
selfChange = true;
counter++;
window.location.hash = "#" + hashLocation + counter;
document.getElementById("container").innerHTML = window.location.hash.split("#")[1];
}
addEventListener('hashchange', (event) => {
if (!selfChange) {
document.getElementById("container").innerHTML = window.location.hash.split("#")[1];
}
selfChange = false;
});
html:
<a href="javascript: next();">add +</a>
<div id="container">
</div>
您可以使用history.pushState(null, document.title, window.location)
。当您使用此选项时,它将清除前向历史。