Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear html history created by pushState on refresh

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.

like image 811
jcubic Avatar asked Apr 09 '14 20:04

jcubic


People also ask

Does history pushState refresh the page?

The history. pushState() method can be used to push a new entry into the browser's history—and as a result, update the displayed URL—without refreshing the page. It accepts three arguments: state , an object with some details about the URL or entry in the browser's history.

How do I uninstall pushState?

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.

What does Window history pushState do?

In an HTML document, the history. pushState() method adds an entry to the browser's session history stack.

How do I reset my windows history?

Press Ctrl-Shift-Delete to bring up the history-clearing options. It brings up a list of what you can and cannot delete, click the Details arrow if you want a more detailed list. You can also choose a period to clear.


3 Answers

My idea - override whole history:

for (i = 0; i < 50; i++) {
  history.pushState({}, '');
}
like image 82
Luke Duda Avatar answered Oct 23 '22 22:10

Luke Duda


There is no way to clear the session history or to disable the back/forward navigation from unprivileged code. The closest available solution is the location.replace() method, which replaces the current item of the session history with the provided URL. Mozilla

The only way is this (but I think you know it)

var length=history.length;     
history.go(-length);
window.location.replace("urlToRedirect");

after using replace() the current page will not be saved in session history, meaning the user won't be able to use the Back button to navigate to it.

like image 30
faby Avatar answered Oct 23 '22 22:10

faby


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.

like image 20
Gabriele Petrioli Avatar answered Oct 23 '22 21:10

Gabriele Petrioli