2013-11-01 770 views
2

我在我的网页中使用​​3210。它工作正常,但是当用户点击页面上的面包屑时,直接进入第一页,也就是没有点击浏览器后退按钮,History.js堆栈未被清除,并且第一页上的noe按下按钮无法正常工作。 有没有一种方法可以清除History.js堆栈。 在Html5历史api中,我会做history.go(-(history.length - 1));,但是当使用History.js History.length总是给我0,即使我跟着面包屑并跳过了一些后退按钮点击。清除History.js的历史堆栈

回答

7

引述另一篇文章

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

有一个解决方案是这样的:

var Backlen=history.length; 

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

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

Source

+0

我们没有history.length在History.js。我没有使用html5 history api,但为了在html4浏览器中兼容,使用History.js(https://github.com/browserstate/history.js/) – vishesh

+0

您可以在github上打开一个问题来解决问题。 –

0

这是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); 
+0

在某些情况下,这是有效的,但其他情况则不行。每次有替换状态时,它都会增加索引。因此,如果有替换状态是下一个索引,则第一页是初始文章。所以回到第一页会回到第二个索引,因为那是第一页的最后更新。 – deefactorial

2

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

此解决方案在我的情况:

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