iframe 和父站点之间如何通信? [英] How to communicate between iframe and the parent site?

查看:26
本文介绍了iframe 和父站点之间如何通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iframe 中的网站不在同一个域中,但两者都是我的,我想在 iframe 和父站点之间进行通信.可能吗?

The website in the iframe isn't located in the same domain, but both are mine, and I would like to communicate between the iframe and the parent site. Is it possible?

推荐答案

对于不同的域,无法直接调用方法或访问 iframe 的内容文档.

With different domains, it is not possible to call methods or access the iframe's content document directly.

您必须使用跨文档消息.

例如在顶部窗口中:

myIframe.contentWindow.postMessage('hello', '*');

在 iframe 中:

and in the iframe:

window.onmessage = function(e) {
    if (e.data == 'hello') {
        alert('It works!');
    }
};

iframe ->父母

例如在顶部窗口中:

iframe -> parent

For example in the top window:

window.onmessage = function(e) {
    if (e.data == 'hello') {
        alert('It works!');
    }
};

在 iframe 中:

and in the iframe:

window.top.postMessage('hello', '*')

这篇关于iframe 和父站点之间如何通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆