Can't go back after changing window.location.href

穿精又带淫゛_ 提交于 2020-01-06 06:10:40

问题


In my script I'm tracking what tab I am on in a web page using

window.location.href = #!hashName1

If I then click on another tab, it will go to #!hashName2

My issue is, if I click the back button, it just goes back to the state #!hashName1. I have to then click back once again to go back another page.

Is there any way to just have it go back a page and not back to the previous hash state?

Thanks


回答1:


u can handle back button event like this

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

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



回答2:


this will fire whenever you press back button and add previous url

 window.onbeforeunload = function() {
            window.location.href = document.referrer;
    }



回答3:


The following worked perfectly:

I changed:

window.location.href = #!hashName1

to

history.replaceState("", "", #!hashName1);


来源:https://stackoverflow.com/questions/53112946/cant-go-back-after-changing-window-location-href

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!