问题描述
我有一些页面包含iframe。我想在iframe中添加一个链接/按钮,以使浏览器返回历史记录中的一页。但我希望PARENT返回,而不是iframe本身。
I have some pages with iframes in them. I want to add a link/button inside the iframe, to make the browser go back one page in history. But I want the PARENT to go back, not the iframe itself.
我最初有这个,这使得iframe页面返回(如果存在):
I originally had this, which makes the iframe page go back (if it exists):
<a href="javascript:history.back()">« Go back</a>
我试过 window.parent.history.back()
和 window.parent.document.history.back()
但两者都不起作用。从父级访问iframe没有跨域问题,反之亦然。
I've tried window.parent.history.back()
and window.parent.document.history.back()
but neither one works. There are no cross-domain issues accessing the iframe from the parent and vice-versa.
推荐答案
我做了一些玩弄尝试回答这个问题 - 至少在IE8中。
I've done some toying around to try and answer this question - at least in IE8.
iframe中的导航似乎会影响父级的历史记录。我通过在父页面中和使用alert显示history.length值的链接子页面中添加代码来测试这一点。导航到iframe中的新窗口会导致history.length在父页面和子页面上递增。
Navigation within the iframe appears to impact the parent's history. I tested this by adding code within the parent page and within the linked child pages that uses alert to show the value of history.length. Navigating to a new window within the iframe causes history.length to increment on the parent page and on the child page.
我能想到控制后退行为的唯一方法如你所愿,将使用history.go(XXX),其中XXX是一个硬编码的数字,或者是通过跟踪加载iframe后的页面加载数量得出的。
The only way I can think to control the back behavior as you desire would be to use history.go(XXX) where XXX is a hard-coded number or is derived by keeping track of the number of page loads since they loaded the iframe.
如果可以的话,我的个人建议是探索iframe的替代方案 - 这种方法有一点代码味道。 :)
My personal suggestion would be to explore an alternative to iframe if you can - this approach has a bit of a code smell to it. :)
这篇关于iframe父窗口的Javascript后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!