javascript 在父窗口调用 iframe 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16336865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 04:14:51  来源:igfitidea点击:

Call iframe function in parent window

javascriptjqueryiframe

提问by Mangesh Narayankar

How can i call iframe function in parent window, i did something like below but not seems working in firefox. Same code working perfectly in chrome.

我如何在父窗口中调用 iframe 函数,我做了类似下面的事情,但似乎在 Firefox 中不起作用。相同的代码在 chrome 中完美运行。

window.frames["original_preview_iframe"].window.exportAndView(img_id);

回答by radha krishna

i think you have to use

我认为你必须使用

document.getElementById('target_Frame').contentWindow.callingtargetFunction();

document.getElementById('target_Frame').contentWindow.callingtargetFunction();

otherwise use this url describes solution for your problem

否则使用此 url 描述您的问题的解决方案

Invoking JavaScript code in an iframe from the parent page

从父页面调用 iframe 中的 JavaScript 代码

回答by thebreiflabb

Try to not type window after you 'selected' the iframe:

在“选择”iframe 后尽量不要输入 window:

window.frames["original_preview_iframe"].exportAndView(img_id);

回答by Mihkel L.

Would suggest this https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

会建议这个https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

Clear wiki example that worked for me:

清晰的 wiki 示例对我有用:

var o = document.getElementsByTagName('iframe')[0];
o.contentWindow.postMessage('Hello B', 'http://example.com/');

And then in the iframe:

然后在 iframe 中:

function receiver(event) {
    if (event.origin == 'http://example.net') {
        if (event.data == 'Hello B') {
            event.source.postMessage('Hello A, how are you?', event.origin);
        }
        else {
            alert(event.data);
        }
    }
}
window.addEventListener('message', receiver, false);

(https://en.wikipedia.org/wiki/Web_Messaging.)

https://en.wikipedia.org/wiki/Web_Messaging。)