سؤال

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.

هل كانت مفيدة؟

المحلول

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

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 );
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top