【问题标题】:Chrome forces page refresh on window.history.back()Chrome 强制在 window.history.back() 上刷新页面
【发布时间】:2016-10-07 14:50:59
【问题描述】:

我正在处理没有框架的单页表单。不使用框架的简单单页。我正在使用带有 history.pushState 的哈希系统,因此根据单击的 div,我分配了不同的哈希。例如:

        var stateObj = { id: "file" };


        $(document).on('click', "#foo", function () {
                                showTemplate(templates_main, data);
                                history.pushState(stateObj, "page 1", '#foo')

        }),

        $(document).on('click', "#faa", function () {
                                showTemplate(templates_main, data);
                                history.pushState(stateObj, "page 1", '#faa')

        }),

在提交表单之前,pushState 在两个浏览器上的行为都是一样的。

  • 页面 URL == index.html
  • 点击#foo,URL == index.html#foo
  • 点击#faa URL == index.html#faa
  • 点击返回的网址应该是index.html#foo

提交表单后,使用 perl 创建一个新页面。如果给定的选项有误,创建的页面将包含一个按钮,该按钮会触发window.history.back() 以重试。

这适用于 Firefox,它还会在您单击返回时返回所有输入的数据。

问题从Chrome开始,提交表单并按下返回按钮后,我可以看到Chrome先到index.html#foo然后自动刷新页面,导致URL == index.html。

我知道如果你想让 Firefox 表现得像 Chrome 或 IE,你可以在正文中添加onunload=""。但是有什么我可以添加到正文中或者我可以做些什么来让 Chrome 像 Firefox 一样运行并避免重新加载页面吗?

  • Chrome 为什么要这样做?
  • 如何避免页面刷新?或
  • 在重新加载页面后,如何在执行history.back() 时将页面更改为原始状态?

类似:

onclick="window.location.href=document.history.length(-1)"

谢谢,

【问题讨论】:

    标签: javascript google-chrome browser-history pushstate


    【解决方案1】:

    对我来说,Chrome 仅在完整文件上传后触发 history.back() 时才会强制刷新页面。

    【讨论】:

      猜你喜欢
      • 2016-02-08
      • 2015-07-13
      • 2021-10-28
      • 1970-01-01
      • 2013-01-18
      • 2012-02-09
      • 2018-06-19
      • 2013-12-05
      相关资源
      最近更新 更多