You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用history.pushState()时出现历史记录异常(重复、丢失或被修改)的问题求助

使用history.pushState()时出现历史记录异常(重复、丢失或被修改)的问题求助

各位大佬好,我最近在项目里用history.pushState()处理历史记录的时候碰到了挺头疼的问题,想请大家帮忙分析下原因。

先贴出我的相关代码:

$.ajax({
  url: link,
  type: 'get',
  data: {},
  success: function(res) {
    document.title = res.title;
    document.description = res.description;

    $('body').html(res.html);
    addToHistoryUrl(res, link);
  }
});

function addToHistoryUrl(res, url) {
  window.history.pushState({
    'title': res.title,
    'description': res.description
  }, "", url);
}

这段代码大部分时候运行都正常,但偶尔会出现异常情况:pushState()会在历史记录里创建重复条目,有时候会丢失部分记录,甚至还会修改已有的历史记录。

补充说明下,我没有使用onpopstate()事件处理函数,页面的前进、后退等导航操作都是完全依赖浏览器原生机制处理的,没自己额外加逻辑。

备注:内容来源于stack exchange,提问作者NomadCode

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 19:53:09