最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript back button for iframe parent window - Stack Overflow

programmeradmin5浏览0评论

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.

I originally had this, which makes the iframe page go back (if it exists):

<a href="javascript:history.back()">&laquo; Go back</a>

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.

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.

I originally had this, which makes the iframe page go back (if it exists):

<a href="javascript:history.back()">&laquo; Go back</a>

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.

Share Improve this question asked Jun 7, 2010 at 21:17 DisgruntledGoatDisgruntledGoat 72.7k70 gold badges212 silver badges291 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

I've done some toying around to try and answer this question - at least in IE8.

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.

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.

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. :)

You can use this:

    <a href="javascript:self.history.back()">back</a>

    or:

    <a href="javascript:self.history.go(-1)">back</a>

It's work for me!

;)

发布评论

评论列表(0)

  1. 暂无评论