Asked
Viewed
28.3k times

I have a page on my site which has a menu with a list of links. The links are displayed in an IFRAME.

Underneath the links there is a "back" button which calls, via a hyperlink, "JavaScript:history.back()"

Unfortunately the back button scrolls back through all the iframe's history, meaning it has to be clicked several times before the previous page actually reloads, most annoying.

Anybody know how I can get the back button to ignore the history of the iframe?

add a comment
0

6 Answers

  • Votes
  • Oldest
  • Latest
GR
0 0
Answered
Updated

I think you'd be served better by not adding the URLs for your iframe navigation activities into the webpage's history, with menu items constructed like this

<a href="next.html" onclick="location.replace(this.href); return false">next</a>
add a comment
0
Answered
Updated

Hmm an elegant solution but it doesn't appear to work.

Ive tried both:

<a href="band.php" onclick="location.replace(this.href); return false" target="news">

and

<a href="band.php" target="news"  onclick="location.replace(this.href); return false">

Whilst they solve the history issue, the target appears to be totally ignored and
the page loads in the main window 😟

Any idea why that would be happening?

add a comment
0
Answered
Updated

Have you tried using parent.history.back(); ?

add a comment
0
Answered
Updated

just tried it and that doesn't work.

Although I wouldn't have expected it too. The back button is not even contained within the iframe, it's contained within the parent frame.

add a comment
0
GR
0 0
Answered
Updated

Pleng, the problem you describe with my suggestion above seems to be that the JavaScript executed by the click handler is running under the context of the main window, rather than the window of the iframe that (presumably) contains the anchor element being clicked. Thus the window being navigated is the window for the entire document, rather than the window for the iframe. Offhand I don't understand why that would be, and that's not the way it works in a simple test case I made up.

I can think of these things to try:

1) Add an anchor element built as I suggested as a first-level child of the iframe, outside of your menu. That's how my simple, working example is constructed. See if that works, while the presumably more complex example you're currently using does not. If so, try to figure out what's different.

2) Try harder to force the navigation to affect the iframe's window, rather than the main window. Since for some reason the anchor element's click handler is running in the context of the main window, add a navigation function to the main window. So constuct the anchor so:

<a href="next.html" onclick="navigateFrame(this.href); return false">next</a>

Implement that function in the main window

function navigateFrame(url) {
  var w = document.getElementById("thatPeskyFrame")
  w.contentWindow.location.replace(url)
}

and of course if you do it as I've suggested, you'll have to tag the iframe in question

<iframe id="thatPeskyFrame" etc.>

All this is the same as my earlier suggestion, but it's targeted explicitly at the iframe.

Note that contentWindow is a well documented member of the iframe DOM element. It's a Microsoft standard, not a W3 standard. However I think it's supported by all browsers. Firefox supports it. I haven't tried Opera, but Opera tries hard to be IE-compatible. I don't know about other browsers. On the other hand, if the script fails for any reason, it will fail gracefully and simply navigate the main window using the default method. That will give you history entries, but at least it will work.

3) The window.history object can be navigated back more than one element at a time. You could live with the undesired URL history being added to the main window, keep track of the number of history entries the user has caused to be added, and change your Back button's script so that it skips back that number of entries. You won't be able to manipulate the browser's Back button. I can't imagine that any browser would allow HTML content to affect its behavior.

add a comment
0
Answered
Updated

Grey thank you so much, your script has made the page work exactly as required!

The reason the "back" button is in the parent frame rather than the IFRAME, just out of interest (you said you couldn't see why this would be the case), is that the page is a news page. The user can click on either site news, corporate news, etc... or back to navigate back to the homepage... the news links will load up something in the iframe, where as the back link needs to bring the whole page back to the calling page.

Thanks again for your help. It's just the ticket!

add a comment
0