0

I am trying to get an alert to fire when I press back to get to a page from a hash. I'm using the following code

if(window.location.hash) {
} else {
    alert('test');
}

If I'm at http://someurl.com/#somehash and I press the browser back button to get back to http://someurl.com/ the alert doesn't fire.

Is there any way to make that work, or am I misunderstanding something?

Thanks a lot!

James Allardice
  • 164,175
  • 21
  • 332
  • 312
Martin Hunt
  • 1,135
  • 2
  • 14
  • 23

1 Answers1

2

Here is you question already answered.

It boils down to using a listener:

window.onhashchange = function() {
  console.log('hash changed');
}
Community
  • 1
  • 1
Undefined
  • 11,234
  • 5
  • 37
  • 62