0

What I'm trying to achieve is to have this script run even after navigating back from #bar to #foo

if ( window.location.href.indexOf('#') > -1 ) {
   var url = window.location.hash.substr(1);
   alert(url);
}

It is similar to this https://stackoverflow.com/questions/2638292/after-travelling-back-in-firefox-history-javascript-wont-run but the difference is that I will be navigating back to the same page but with diferent anchor.

Is this possible to achieve this with frameworkless javascript?

I saw Gmail have this router-like behavior with the use of anchor hash (if you browse through the mail), and that even when navigating back, the page responds.

Community
  • 1
  • 1
  • As far as i know, if you reload the webpage, you reload the javascript, moving to a different part of the page without actually reloading the page keeps it alive. – Dellirium May 26 '16 at 08:05

1 Answers1

1

If you are not reloading the page you can do this with an event listener.

window.addEventListener('hashchange', function () {
    // do stuff
});
Lars Graubner
  • 817
  • 4
  • 8