views:

1796

answers:

1

I'm using an iframe to display content that has links. When the user clicks around in the iFrame and hits "back," it goes back in the iFrame. This behavior is OK. However, once they're back to the first page of the iFrame and they hit "back" again, the entire window is taken back to the previous page. This is unwanted.

To prevent this behavior, I've put a fake "back" button within the iFrame. (In most cases this is bad UI, in this case, it works well). I'd like this fake back button to only go back if the previous page is the iFrame's page -- not the entire page. When they hit the fake back button in the iFrame, it should only move that iFrame back, nothing else. Is there a way to do this? Does an iFrame get its own history object?

Something that might be of benefit: the domain of the iFrame and the main window can be assumed to be distinct. So, if it's possible to read the "global" history object, I can check to see if the previous page was mine by checking to see if the domain is mine. If the domain is not mine, the fake back button will be hidden or not do anything.

Help greatly appreciated, and happy holidays!

+2  A: 

You should be able to use the javascript history object to push the user back; but you won't be able to stop it when the iframe-clicking runs out and the main page wants to go back. And you can't stop it because that's intentionally locked down pretty well in most browsers to prevent people from messing around with it maliciously.

  • You could write your own history tracking code and have the back button pop items off that stack, stopping when the stack is empty...
  • If you're using some complicated nesting of links - perhaps some javascript-based tree menu? That way the iframe never has a page refresh?

Without having an example, I have to say your design seems like poor UI... when I hit back, I don't want the navigation to change; I want to go back to whatever page I was just on.

Tom Ritter
Thanks... I was hoping there was a nicer way to do this. I guess I'll keep a stack in cookie/session. As per the poor UI, it's a floating iFrame within another page. As you go to new pages in the iFrame you often want to go back (without changing the main page).
Sam