Clear html history created by pushState on refresh

jcubic picture jcubic · Apr 9, 2014 · Viewed 25.3k times · Source

I have array of states and index of the array in history.state and on popstate I read the content of the array, it work fine but when I go back to first page and refresh the array is gone (which is normal) but forward history state remain.

Is it possilbe to remove fake history that was created using pushState?

I can't store my state in localStorage because I have functions in my state array.

Answer

Gabriele Petrioli picture Gabriele Petrioli · May 21, 2014

If you read the specification of History API for pushState, in step 4 it states

  1. If the method invoked was the pushState() method:
    Remove all the entries in the browsing context's session history after the current entry. If the current entry is the last entry in the session history, then no entries are removed.

So depending on what you are trying to do, you could make a special case for when first loading the page and push a state (one you will consider your first), and in doing so clear the forward states of the history.