是否可以从iframe中的JS调用父文档中的Javascript函数?

问题描述:

iframe和父文档都在同一个域中.我想做的就是为在iframe中运行的js提供驻留在父文档中的回调函数.这可能吗?

Both the iframe and the parent document are on the same domain. What I would like to do is give the js running in the iframe a callback function that resides in the parent document. Is this possible?

      window.frames["testFrame"].org.myorg.events.onDataReady.addListener(function (e) {
                alert("Data ready!");
}

我希望"testFrame"调用匿名函数.上面的js将在父文档中.这可能吗?

I would like "testFrame" to call the anonymous function. The above js would be in the parent document. Is this possible?

谢谢. 更新

以下作品:

$(document).read(function(){
   $(frameObj).load(function () {
                window.frames["testFrame"].org.myorg.events.onDataReady.addListener(function (e) {
                    var doc = $("#summaryFrame")[0].contentDocument;
                    doc.open();
                    doc.writeln(e.data.summaryHtml);
                    doc.close();
                });
});

是的-在上一份工作中,我已经做过很多次了(...他们的软件"基本上是1,000帧和一个带标签的文档)

Yes you can - I've done this many times before in a previous job (... their "software" was basically 1,000 frames and a tabbed document).

假设您的顶部框中有以下代码:

Assuming you have this code in your top frame:

function blah() { alert("it worked!"); }

您可以在iframe中调用它...

You can call this in the iframe...

top.blah();

此外,您可以让父母调低孩子的身份,但我认为反之则容易些.

Also, you can have the parent call down into the child, but the other way around is easier in my opinion.