javascript - Chrome 在 window.history.back() 上强制刷新页面

标签 javascript google-chrome browser-history pushstate

我正在处理没有框架的单页表单。不使用框架的简单单页。我正在使用 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
  • 点击返回 URL 应该是 index.html#foo

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

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

问题从 Chrome 开始,因为在提交表单并按下后退按钮后,我可以看到 Chrome 首先转到 index.html#foo 然后自动刷新页面,导致 URL == 索引.html。

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

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

类似于:

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

谢谢,

最佳答案

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

关于javascript - Chrome 在 window.history.back() 上强制刷新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39920118/

相关文章:

Javascript单单引号

javascript - 无法将项目推送到状态内的项目数组

javascript - window.getSelection 返回 undefined 或 null

html - 在连续流上解码音频数据失败并出现空错误

javascript - 检查值是否为空

javascript - post 请求在 Spring Boot 和 AngularJS 之间出现错误的请求错误

javascript - Chrome 滚动条消失(不是 overflow-y)

javascript - React-Router-我可以在React Router的浏览器历史记录中获取下一个路径字符串,还是检查是否存在转发?

javascript - 是否可以从用户那里获取最后 2 个、甚至 3 个 URL?类似于 document.referrer

javascript - Popstate - 将弹出状态传递给事件处理程序