让Ajax网页带有后退功能

< DOCTYPE html PUBLIC -WCDTD XHTML StrictEN httpwwwworgTRxhtmlDTDxhtml-strictdtd>
让Ajax网页带有后退功能

在AJAX 做了一个项目后,测试人员告诉我,新闻列表翻了很多页后,当查看过一条新闻的详细信息返回目录的时候,不能返回到已经翻过的页码,而要从第一页重新翻,这样很郁闷。网上面找到一篇<开发保留标准浏览器功能的AJAX应用程序>的文章,按照里面的介绍,把相应的代码节选出来,保留在这里。

我实现原理是:每翻一次页,把当前的页码存入碎url的碎片标识中,待客户查看完详细信息后返回到目录,在目录的body的onload事件执行setOptionValue,将碎片的内容获取下来,再执行读取目录的函数;以达到保存翻页信息的功能。

//使用URI的碎片标识符部分,创建状态的历史记录;
function makeHistory(newHash)
{
window.location.hash = newHash;
expectedHash = window.location.hash;
alert(expectedHash);
return true;
}
//检查浏览器历史记录中的URI
function handleHistory()
{
if ( window.location.hash != expectedHash )
{
     alert(window.location.hash + "|" + expectedHash);
    expectedHash = window.location.hash;
    var newoption = expectedHash;
    setOptionValue( newoption );
}
return true;
}

function pollHash() {
handleHistory();
//window.setInterval("handleHistory()", 1000);
return true;
}

//获取碎片中的内容,可进行任何操作
function setOptionValue(value)
{
// var myForm = document.make_history;
// var mySelect = myForm.change_year;
// mySelect.options[value-1].selected = true;
    document.getElementByIdx("testDiv").innerHTML = value.replace('#','');
    Test(value.replace('#',''));
    alert(value.replace('#',''));
return true;
}
原文地址:https://www.cnblogs.com/netcorner/p/2912332.html