3

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

Best Dev Tutorials
  • 452
  • 2
  • 6
  • 22

3 Answers3

0

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();
}
jayanth
  • 72
  • 4
  • 1. Wouldn't this be triggered every time the hash is changed? 2. Also, I don't think this would work if I click on multiple tabs (multiple hash updates) – Best Dev Tutorials Nov 02 '18 at 05:20
0

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

 window.onbeforeunload = function() {
            window.location.href = document.referrer;
    }
jayanth
  • 72
  • 4
0

The following worked perfectly:

I changed:

window.location.href = #!hashName1

to

history.replaceState("", "", #!hashName1);
Best Dev Tutorials
  • 452
  • 2
  • 6
  • 22