【发布时间】: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