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
I like this replaceState, I hadn’t thought about it, it’s a good.
– Ivan Ferrer