javascript 清除 History.js 的历史堆栈

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19732486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 16:38:47  来源:igfitidea点击:

clear History stack of History.js

javascripthistory.js

提问by vishesh

I am using History.jsin 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.lengthgives me 0 always,even if I followed breadcrumb and skipped some of the back button clicks.

我在我的网页中使用History.js。它工作正常,但是当用户单击页面上的面包屑直接进入第一页时,即没有单击浏览器后退按钮,History.js 堆栈不会被清除并且没有按回第一页上的按钮无法正常工作。有没有办法清除 History.js 堆栈。在 Html5 history api 中我会这样做,history.go(-(history.length - 1));但是当使用 History.js 时History.length总是给我 0,即使我跟随面包屑并跳过一些后退按钮点击。

回答by Donovan Charpin

There is no way to clear the session historyor 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

无法清除会话历史记录或禁用非特权代码的后退/前进导航。最接近的可用解决方案是location.replace()方法,它用提供的 URL 替换会话历史记录的当前项目。Mozilla 源

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

来源

回答by reto lee

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.

多诺万的回答很好,但在我的情况下不起作用。backlength 索引太高了,因此,命令 history.go(-Backlen) 将被忽略。

This solution works in my case:

这个解决方案适用于我的情况:

var backlen = history.length - 1;  
history.go(-backlen); // Return at the beginning
history.replaceState({}, null, 'your relative url');

回答by cbr

I don't know the direct answer, but you could try window.location.replace(url);.

我不知道直接的答案,但你可以试试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)?

引用另一篇文章:在使用 replace() 后,当前页面将不会保存在会话历史记录中,这意味着用户将无法使用“后退”按钮导航到它 在 Javascript 中,我如何“清除”后退(历史-1)?

回答by deefactorial

This is the History.js equivalent.

这是 History.js 的等价物。

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