title: replaceState before back/forward button click
body: <p>I need to call <code>replaceState</code> before the user leaves the current page (e.g. back/forward button click) so the history has the most current state the next time the page is loaded on <code>popstate</code>. How to?
  <hr/>
  I guess the alternative would be to call <code>replaceState</code> everytime the state changes, but that sounds kinda messy to me.
  <hr/>
  Found a <a href="https://stackoverflow.com/a/10703979/39923">workaround</a>. A "pre" popstate event would be ideal for this.</p>