使用Javascript从iframe获取对象

使用Javascript从iframe获取对象

问题描述:

大家好
我的页面上有一个iframe.现在我想从iframe中获取对象或在父对象中调用Java函数.

我能怎么做?

Hi all
I have an iframe in my page. now i want get an object or call a java function in parent from iframe.

how can i do? is possible?!

如果我正确理解了您的问题,您想从同一页面的IFrame调用父页面方法. YES you can.
假设您具有以下结构

If I understood your question correctly, you want to call a Parent Page method from IFrame in the same page. YES you can.
Let say you have below Structure

[mainpage markup]
   [main page JS ]
       function TestMethod()
          {
          }
[main page JS ]
        [iframe id="myiframe"]
         [/iframe] 
[mainpage markup]


然后您可以按以下方式调用TestMethod


Then you can call the TestMethod as below

document.getElementById(''myiframe'').contentWindow.TestMethod();



contentWindow将返回您的父容器.希望这对您有帮助!



contentWindow will return you parent container. Hope this helps !!