2010-07-25 32 views
1

我刚刚注意到,我的JavaScript历史控件针对iFrame正在影响父母。该代码是:JavaScript历史控件针对iFrame影响父母

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

它正常工作,直到有没有回去或在iframe中,然后其会影响父框架移动即向前和向后,这是在所有主要浏览器出现的正向, safari,opera,chrome,firefox,ie 6 7 8.

任何人都知道如何阻止它?

回答

1

检查history对象的length第一:

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

条件才能运行的时候也有一些是为history对象中导航。

+0

这似乎不断返回值38? – cappuccino 2010-07-25 09:28:11

+0

检查历史长度不起作用,因为它包含父框架的历史长度。 – cappuccino 2010-07-25 10:51:18

+0

@ cappuccino:当你执行alert(window.history.length)时,返回什么值? – Sarfraz 2010-07-25 10:55:27