clear History stack of History.js

13,504

Solution 1

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 Source

There is a solution like this :

var Backlen=history.length;   

history.go(-Backlen); // Return at the beginning

window.location.replace("page url to redirect");

Source

Solution 2

Donovan's answer is great but does not work in my case. The backlength index is one too high and therefore, the command history.go(-Backlen) will be ignored.

This solution works in my case:

var backlen = history.length - 1;  
history.go(-backlen); // Return at the beginning
history.replaceState({}, null, 'your relative url');
Share:
13,504
vishesh
Author by

vishesh

Founder at CloudFiles

Updated on July 10, 2022

Comments

  • vishesh
    vishesh almost 2 years

    I am using History.js in my webpage.It's working fine,but when user click the breadcrumb on the page comes to the first page directly,i.e.,without clicking browser back button, the History.js stack is not cleared and noe pressing back button on first page doesn't work properly. is there a way I can clear the History.js stack. In Html5 history api I would do history.go(-(history.length - 1)); but when using History.js History.length gives me 0 always,even if I followed breadcrumb and skipped some of the back button clicks.