2013-03-26 30 views
0

我的HTML页面中有两级父 - 子iframe层次结构。我想在其子文档中获取父窗口文档的对象以进行一些操作。我主要与Google Chrome合作。parent.document在Chrome中未定义

parent.document在Google Chrome中给出'undefined',而在Mozilla中它可以正常工作。有什么收获?

仅供参考,请找三个文件证明问题的内容下方,

第一个文件: 'one.html'

<html> 
<head> 
</head> 
<body> 
    <input type="hidden" id="one_1" name="one_1" /> 
    <iframe id="one" name="one" src="two.html"> 
</body> 
</html> 

第二个文件: 'two.html'

<html> 
<head> 
</head> 
<body> 
    <input type="hidden" id="two_1" name="two_1" /> 
    <iframe id="two" name="two" src="three.html"> 
</body> 
</html> 

第三个文件: 'three.html'

<html> 
<head> 
<script type="text/javascript"> 

    function callme() { 
     alert(parent.document) 
     alert(top.document) 
    } 
</script> 
</head> 
<body> 
    <input type="hidden" id="three_1" name="three_1" /> 
    <button id="click" name="click" onclick="return callme()">Click Me</button> 
</body> 
</html> 

假设“one.html”打开谷歌浏览器,当我点击“点击我”按钮,两个连续的警报框将出现与“未定义”值。当我在Mozilla中打开'one.html'时,会出现两个'objectHTMLDocument'值的警告框。

请找到控制台消息之下,而点击“点击我”按钮,提前

Unsafe JavaScript attempt to access frame with URL file:///C:/Users/user/Desktop/two.html from frame with URL file:///C:/Users/user/Desktop/three.html. Domains, protocols and ports must match. 
                   three.html:6 
callme               three.html:6 
onclick               three.html:13 

Unsafe JavaScript attempt to access frame with URL file:///C:/Users/user/Desktop/one.html from frame with URL file:///C:/Users/user/Desktop/three.html. Domains, protocols and ports must match. 
                   three.html:7 
callme               three.html:7 
onclick               three.html:13 

感谢。

+0

这个问题是['contentDocument'](https://developer.mozilla.org/en-US/docs/HTML/Element/iframe#Scripting)。 – bfavaretto 2013-03-26 20:08:01

+0

@bfavaretto刚刚检查过contentDocument,仍然没有收获:( – dsharma4u29 2013-03-26 20:09:39

+0

您能否显示您的代码? – bfavaretto 2013-03-26 20:10:45

回答

0

我会从上而下注入,而不是自下而上。我将设置在一个iFrame的变量通过选择它,并将其存储到一个变量,像这样:

var frame = document.getElementById('one'); 

然后注入到父的引用:

frame.contentWindow.frame_parent_reference = window; 

然后在下一执行此儿童iFrame用“two”代替“one”。这样一来,通过third.html,我们不要求父母或顶部,但能做到以下几点:

alert(frame_parent_reference.document); 
alert(frame_parent_reference.frame_parent_reference.document); 

也许不是超级优雅,但它肯定给你很多控制的(你可以检查自定义引用存在安全性)。

祝你好运!

相关问题