如何从iframe访问父窗口中的JavaScript对象

ias*_*ons 4 iframe firefox internet-explorer

我有一个网页,其中包含一个名为"myVariable"的JavaScript变量.

在此网页中是一个指向第二个网页的iframe.

两个页面都在同一个域中.

iframe中的页面需要访问父网页中定义的"myVariable".

我使用以下代码使用Firefox成功访问了此变量:

document.defaultView.parent.myVariable 
Run Code Online (Sandbox Code Playgroud)

但是,这在Internet Explorer中不起作用.....

我如何在Internet Explorer中执行此操作?

Thi*_*ilo 5

如果两个页面都来自同一个域(就像你说的那样),window.topwindow.parent应该可以到达父框架.

window.top.document.myVariable
Run Code Online (Sandbox Code Playgroud)