首页 > 解决方案 > 更改 window.location.href 后无法返回

问题描述

在我的脚本中,我正在跟踪我在网页中使用的标签

window.location.href = #!hashName1

如果我然后单击另一个选项卡,它将转到 #!hashName2

我的问题是,如果我单击后退按钮,它只会回到状态 #!hashName1。然后我必须再次单击返回以返回另一个页面。

有没有办法让它回到一个页面而不是回到以前的哈希状态?

谢谢

标签: javascripthashwindow.location

解决方案


你可以像这样处理后退按钮事件

window.onhashchange = function() {
  goBack();
}

function goBack() {
    window.location.hash = window.location.lasthash[window.location.lasthash.length-1];
    //blah blah blah
    window.location.lasthash.pop();
}

推荐阅读