当我的页面位于 iframe 中时,我注意到 Internet Explorer 的一种奇怪行为。似乎 iframe 在调用 window.history.back() 时会自行重新加载,即使只有 URL 中的哈希值应该改变。当页面不在 iframe 中时,它会正常运行并且不会重新加载页面。知道为什么会发生这种情况以及如何预防吗?
我创建了一个 fiddle ,将在 IE9 中演示这一点:
http://jsfiddle.net/peh96/5/
jsfiddle 使用 iframe,因此行为将是 iframe 行为。单击“#foo”和“#bar”将更改 URL 中的哈希值。现在单击“返回”链接将触发 window.history.back()。请注意,当您这样做时时间戳会发生变化,表明页面正在重新加载。
或者,如果您直接加载 iframe:
http://fiddle.jshell.net/peh96/5/show/
您会注意到单击“返回”时时间戳不会改变。
这只是一个 IE 问题,因为无论是否在 iframe 内,Chrome 和 Firefox 都是一致的。
知道如何防止这种重新加载吗?
最佳答案
好吧,当我从上下文菜单中选择后退命令时,它也是如此。在 IE10 中,您可以使用 HTML5 State Management .恐怕在 IE9 中您必须跟踪哈希历史,然后像这样更改它:
document.getElementById('back').addEventListener('click', function () {
window.location.hash = 'abc';
}, false);
编辑
那这个呢?
当您调用
javascript:window.top.location.hash='bar'
,你可以抓到onhashchange
父窗口中的事件,然后调用 scrollTo
在 iframe 中。但这仅适用于同一域。
关于internet-explorer - window.history.back() 在 iframe 中重新加载 Internet Explorer 中的页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11040768/