tags:

views:

73

answers:

1

I've just noticed that my JavaScript history control targeted at a iFrame is affecting the parent. The code is:

document.getElementById('iframeid').contentWindow.history.back(-1);
document.getElementById('iframeid').contentWindow.history.forward(-1);

It works fine, until there is nothing to go back or forward in the iframe, where then it will affect the parent frame moving that backwards and forwards, this is occurring on all major browsers, safari, opera, chrome, firefox, ie 6 7 8.

Anyone know how to stop it?

+1  A: 

Check the history object's length first:

if (document.getElementById('iframeid').contentWindow.history.length)
{
   // your code....
}

The condition will run only when there is something for the history object to navigate around.

Sarfraz
this seems to constantly return the value 38?
cappuccino
Checking the history length doesn't work since it includes the history length from the parent frame.
cappuccino
@cappuccino: What value is returned when you do `alert(window.history.length)` ?
Sarfraz