我試圖在會話存儲中存儲頁面訪問歷史記錄。 會話存儲工作正常,因爲我想要的。如何在會話存儲中使用相同的密鑰更新陣列
問題: 當我訪問了1,2,3,第4頁,然後再次訪問第2頁和第三頁,然後它不與再次第2和第3頁更新陣列。
不在數組的第2頁和第3頁追加數據,這些已經存在但我已經在這些頁面上再次訪問,那麼它應該被添加到數組中。 。
我想存儲每個頁面訪問歷史數組。
if (window.sessionStorage) {
var currentTite = document.title;
var currentURL = window.location.href;
sessionStorage.setItem(currentTite, currentURL);
//var storedData = sessionStorage.getItem(currentTite);
for (i = 0; i <= sessionStorage.length - 1; i++) {
key = sessionStorage.key(i);
console.log(key);
val = sessionStorage.getItem(key);
console.log(val);
}
}
這是帶有示例html頁面的代碼示例。 名稱:simplestyle_banner.tar.gz
http://expirebox.com/download/34893b0eea4538b039c913993877393c.html
我怎麼能簡單地在控制檯中獲得所有的細節? –
@PuzzledBoy只需使用'console.log(sessionStorage.getItem('history'));'。 –
@PuzzledBoy用你的問題中的'currentTite'變量替換''history''並沒有什麼錯誤。我會這樣做,如果你的JavaScript代碼是從外部文件加載,並在多個頁面中使用。 – Mottie