javascript:window.document返回null

javascript:window.document返回null,javascript,Javascript,我有一个名为“welcome.HTML”的HTML文档,它使用一个引用“main.HTML”的iframe。在“inner.html”文件中,使用另一个iframe引用“inner.html”。现在在'inner.html'中,我想访问main.html的元素或在main.html的元素之间进行通信。我在javascript中使用了以下方法: 方法1: .. .. var top_window = window.top; alert(top_window) // gives top win

我有一个名为“welcome.HTML”的HTML文档,它使用一个引用“main.HTML”的iframe。在“inner.html”文件中,使用另一个iframe引用“inner.html”。现在在'inner.html'中,我想访问main.html的元素或在main.html的元素之间进行通信。我在javascript中使用了以下方法:

方法1:

..
..
var top_window = window.top;
alert(top_window)    // gives top window object
var main_window = top_window.frames['main-frame'] // assuming that 'main-frame' is the name given at the iframe declaration for 'main.html'
alert(main_window)    // gives main window object
var main_document = main_window.document
alert(main_document)    // gives 'undefined'
..
..
方法2:

..
..
var parent_window = window.parent;
alert(parent_window)    // gives parent window object, that is, 'main.html' window object
var parent_document = parent_window.document
alert(parent_document)    // gives 'null'
..
..
我想要'main.html'的窗口对象的文档对象。我的两种方法都行不通。是否有合理的解决方案,或者我的做法是否错误


提前感谢。

iframe之间最简单的通信方式是获取窗口对象。在父对象和子对象中创建getter setter函数,并在两个窗口对象之间传递值

这将返回iframe的窗口对象

var contentWindow = document.getElementbyId('iframe').contentWindow 

从iframe使用
窗口访问父窗口。父窗口虽然HTML5 doctype支持iframe,但我强烈反对使用它。您的代码是否在
文档之后开始。onload
?您能指出iframe的结构吗?欢迎使用.html->main.html->?html?框架是否打开同一个域?如果没有-这可能是问题所在。是否加载了文档?top和frame的URL是否在同一个域中?