我有一个搜索页面,其中每个搜索结果都添加到具有AJAX的页面。这样我可以让用户搜索例如Led Zeppelin,然后做另一个搜索Metallica,但添加到与以前的搜索相同的结果列表。
我的问题是当用户点击记录的链接,然后点击返回按钮,回到搜索结果。
FireFox(7)保持页面看起来当我离开它,显示完整的结果。
另一方面,IE(7,8)和Chrome(15)会在添加任何使用AJAX的搜索结果之前显示该页面,因为它不记得我向其中添加了数据。
下面是我使用的代码。我试图添加location.hash =“test”;但它似乎没有工作。
// Add search result
$("#searchForm").submit(function (event) {
//location.hash = "test";
event.preventDefault();
$.post($(this).attr('action'),$(this).serialize(),function (data) {
$("tbody").append(data);
});
});
我不需要一个后退按钮跟踪搜索页面上的更改,例如在添加时逐步浏览每个不同的搜索结果。我只是想让浏览器记住最后的搜索结果,当我点击返回按钮。
解决了
更改为document.location.hash =“最新搜索”没有更改任何内容。我不得不使用localStorage作为Amir指出。
这进入jQuery代码的其余部分:
// Replace the search result table on load.
if (('localStorage' in window) && window['localStorage'] !== null) {
if ('myTable' in localStorage && window.location.hash) {
$("#myTable").html(localStorage.getItem('myTable'));
}
}
// Save the search result table when leaving the page.
$(window).unload(function () {
if (('localStorage' in window) && window['localStorage'] !== null) {
var form = $("#myTable").html();
localStorage.setItem('myTable',form);
}
});