views:

284

answers:

1

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.

+1  A: 

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

Mayo