Is it possible to delete the browser history?

Asked

Viewed 123 times

1

Is there any way to delete all browser history via javascript?

The browser is accumulating about 46 histories on my site, and I can’t reduce it even with angry prayer:

I’ve tried with:

history.pushState(null, null, location.href);

With that too:

for (var i=0; i <= window.history.length; i++) {
    window.location.replace('/home');
}

inserir a descrição da imagem aqui

2 answers

6


Responding to:

It is possible to delete browser history?

There is no native way or guaranteed to solve this and no reason to clear user history.

I say this because the browser is the user, even if the site is yours, the user decides how it should and wants to navigate, not you, what you can do is facilitate, a way to avoid history, if it is an internal system, is to do everything in Ajax (IF IT’S AN INTERNAL SYSTEM, IF IT’S A NORMAL SITE, DON’T DO IT, IT’S JUST GONNA GET IN THE WAY).

I must point out, the second code makes no sense:

for (var i=0; i <= window.history.length; i++) {
    window.location.replace('/home');
}

only a direct redirect is forced when it enters the loop, probably this is firing non-stop.

Now you can even try some things, it’s not guaranteed (I mean, I haven’t tested it in all browsers), but what you can try to do is use the history.go() to return to the first and then set the window.location using a hash to differentiate

history.go(-history.length + 1);
setTimeout(function () { location = location.href + '#1'; }, 500);

PS: pushState didn’t work

Or else fix the page:

history.go(-history.length + 1);
setTimeout(function () { location = '/#1'; }, 500);

Remember that the hash is to differentiate the first URL, otherwise it will not differentiate, working as a "Reload" instead of replacing the others

The timeout is just a guarantee, about the load, in some tests I did was necessary, depending on the site/ page.

Of course using #1 on all will not work, so you would have to use a hash that differs from a possible existing hash on the first page, but if that’s the case you could also manipulate querystring con /?querystring=foo&bar=baz&_history=1, exchanging _history={novo valor} Whenever you’re the first, if you don’t have querystring then you’d just be ?_history=1

3

It is not possible to do this (because, if possible, it would represent a kind of "security risk" for users).

It is worth noting that the history.length computes the entire history of session user (from a tab, for example). That way, this number can go far beyond the navigation done on your own website.

Deleting this information would erase the possibility of the user returning to the previous page that he navigated, that is, his Javascript could end up doing an action that harms the user experience in general.


If you have a SPA application (that navigates directly through browser using the API history), you can choose to use the method replaceState, which, unlike the pushState, does not add another item to stack of the historical (therefore the length remains unchanged).

If you’re in an extension environment, you can use the API browser.history.deleteAll. In this case, as the user explicitly installed the extension, this may be allowed to do this type of action.

  • I like this replaceState, I hadn’t thought about it, it’s a good.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.