iframe 父窗口的 Javascript 后退按钮 [英] Javascript back button for iframe parent window
问题描述
我有一些带有 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 中的导航似乎会影响父级的历史记录.我通过在父页面和链接的子页面中添加代码来测试这一点,这些代码使用警报来显示 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 后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!