使用Ajax的后退按钮历史记录,URL中没有散列

Back button history using Ajax without hash in URL

本文关键字:URL 记录 Ajax 历史 按钮 使用      更新时间:2023-09-26

我有这个代码:

    var idRetorno = null;
var link = null;

function handler() 
{
    if(this.readyState == this.DONE) {
    if(this.status == 200) 
    {
           $('#'+idRetorno).html(this.responseText);
           window.history.pushState('', '', link);
    }
  }
}
function link_ajax(linkp, idRetornop)
{
    idRetorno = idRetornop;
    link = linkp;
    var client = new XMLHttpRequest();
    client.onreadystatechange = handler;
    client.open("GET", link);
    client.setRequestHeader('X-Requested-Header', 'XMLHttpRequest');
    client.send();
}

当点击链接时,我的应用程序中的所有东西都会调用函数link_ajax,我试图用更新url

window.history.pushState('', '', link);

但是,后退按钮历史记录并没有像预期的那样工作,页面仍然是一样的。我不能在Url中使用散列(#color:red示例),因为这是应用程序的要求。还有其他解决方案吗?

您是否使用onoptstate来检测页面状态是否已更改?

window.onpopstate = function(event) {
  alert("location: " + document.location + ", state: " + JSON.stringify(event.state));
};