admin管理员组

文章数量:1252793

I am using History.js in my webpage.It's working fine,but when user click the breadcrumb on the page es 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.

I am using History.js in my webpage.It's working fine,but when user click the breadcrumb on the page es 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.

Share Improve this question asked Nov 1, 2013 at 17:56 visheshvishesh 2,0457 gold badges34 silver badges68 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 8 +50

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

Donovan's answer is great but does not work in my case. The backlength index is one too high and therefore, the mand 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');

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 );

本文标签: javascriptclear History stack of HistoryjsStack Overflow