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.

- 2,007
- 6
- 32
- 67
4 Answers
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");

- 3,567
- 22
- 30
-
we don't have history.length in History.js . I am not using html5 history api,but History.js(https://github.com/browserstate/history.js/) for compatibility in html4 browsers – vishesh Nov 05 '13 at 09:30
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');

- 21
- 1
I don't know the direct answer, but you could try window.location.replace(url);
.
To quote another post: 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 In Javascript, how do I "clear" the back (history -1)?
This is the History.js equivalent.
var urlPath = "index.html";
var response = { "html": htmlpage.toString() };
var currentIndex = History.getCurrentIndex();
// Return at the beginning
if( currentIndex > 0 )
History.go( -currentIndex );
//pushState will clear forward history
History.pushState( response , null, urlPath );

- 293
- 1
- 12
-
In certain cases this works but others it doesn't. It increments the index each time there is a replace state. So the first page is the initial post, if there is an replace state that is the next index. so to go back to the first page would go back to the second index, because that was the last update of the first page. – deefactorial Nov 02 '14 at 07:23