我刚在http://ritter.vg上设置了我的新主页.我正在使用jQuery,但非常简单.
它使用AJAX加载所有页面 - 我将其设置为通过检测URL中的哈希来允许书签.
//general functions
function getUrl(u) {
return u + '.html';
}
function loadURL(u) {
$.get(getUrl(u), function(r){
$('#main').html(r);
}
);
}
//allows bookmarking
var hash = new String(document.location).indexOf("#");
if(hash > 0)
{
page = new String(document.location).substring(hash + 1);
if(page.length > 1)
loadURL(page);
else
loadURL('news');
}
else
loadURL('news');
Run Code Online (Sandbox Code Playgroud)
但我不能让后退和前进按钮工作.
有没有办法检测何时按下后退按钮(或检测哈希值何时更改)而不使用setInterval循环?当我尝试使用.2和1秒超时时,它固定了我的CPU.
我有一个搜索页面,其中每个搜索结果都添加到使用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);
});
});
Run Code Online (Sandbox Code Playgroud)
我不需要跟踪按钮来跟踪搜索页面上的更改,例如在添加时逐步浏览每个不同的搜索结果.我只想让浏览器在按下后退按钮时记住上次搜索结果.
已解决
改为document.location.hash = "latest search"没有改变任何东西.我不得不使用localStorage阿米尔指出的那个.
这将进入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 …Run Code Online (Sandbox Code Playgroud)