2013-11-01 780 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');