0

I'm implementing router for navigating in single-page application (something like Angular's ui-router). Routing will be done by changing browser location's hash.

It should be impossible to navigate to an undeclared route. So if I've declared only #/foo route, and currently I'm on #/foo and trying to change hash to #/bar, browser should revert hash to #/foo.

However, after that #/bar will remain in browser's history. The question is: how to completely prevent history changing on hash navigation?

I've tried

$(window).on('hashchange', function(event) {
    event.preventDefault();
    return false;
})

but it didn't work.

1 Answers1

0

This has already been answered. You can use onpopstate event to detect when the user hits forward/back but you will need to wrap the pushState and replaceState methods to detect when they are used. Examples are shown here: How to detect when history.pushState and history.replaceState are used?

Community
  • 1
  • 1
Jim
  • 3,210
  • 2
  • 17
  • 23