erikringsmuth / app-router

Router for Web Components
https://erikringsmuth.github.io/app-router/
MIT License
611 stars 83 forks source link

Handle the dual popstate/hashchange event handlers better #5

Closed erikringsmuth closed 10 years ago

erikringsmuth commented 10 years ago

In some browsers a hashchange also triggers a popstate. As far as I can tell there isn't a check to see if the browser will fire one or both. The temporary fix is to keep track of the previous state (URL) and not load the route if it's to the same previous state.

this.previousState = '';
this.stateChangeHandler = function() {
  if (this.previousState !== window.location.href) {
    this.previousState = window.location.href;
    this.go();
  }
}.bind(this);
window.addEventListener('popstate', this.stateChangeHandler, false);
window.addEventListener('hashchange', this.stateChangeHandler, false);

This needs a better check so that hashchangeand popstate always call router.go().

erikringsmuth commented 10 years ago

More specifically, in IE a hashchange doesn't trigger popstate. It looks like most other browsers do, but detecting IE isn't the best option here either.

http://stackoverflow.com/questions/23423637/ie-not-firing-popstate-when-hashchange-happens

erikringsmuth commented 10 years ago

Closed by https://github.com/erikringsmuth/app-router/commit/471368a44db912ad6918397d343a4b15e0f7e1fd