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

查看:184
本文介绍了如何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:

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

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

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