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

时间:2022-02-23 23:58:47

I have a web page which contains a JavaScript variable called "myVariable".

我有一个网页,其中包含一个名为“myVariable”的JavaScript变量。

Within this web page is an iframe which points to a second web page.

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

Both pages are within the same domain.

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

The page within the iframe needs to access "myVariable" which is defined in the parent web page.

iframe中的页面需要访问父网页中定义的“myVariable”。

I have successfully accessed this variable with Firefox using the following code:

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

document.defaultView.parent.myVariable 

However, this does not work in Internet Explorer.....

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

How do I do this in Internet Explorer?

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

2 个解决方案

#1


If both pages are from the same domain (like you say), window.top or window.parent should work to get to the parent frame.

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

window.top.document.myVariable

#2


Firefox 26.0, Internet Explorer 11, Chrome 32.0:

Firefox 26.0,Internet Explorer 11,Chrome 32.0:

window.top.myVariable

window.top.document.myVariable did NOT work.

window.top.document.myVariable没有用。

#1


If both pages are from the same domain (like you say), window.top or window.parent should work to get to the parent frame.

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

window.top.document.myVariable

#2


Firefox 26.0, Internet Explorer 11, Chrome 32.0:

Firefox 26.0,Internet Explorer 11,Chrome 32.0:

window.top.myVariable

window.top.document.myVariable did NOT work.

window.top.document.myVariable没有用。