发出与postMessage从父级到子级iFrame的通信 [英] Issue communication with postMessage from parent to child iFrame

查看:191
本文介绍了发出与postMessage从父级到子级iFrame的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从父窗口到子iFrame进行通信时遇到问题。但另一方面,一切都很完美。
以下是我获取chil iFrame对象以激发 postMessage 功能的方法:

I'm having an issue communicating from my parent window to the child iFrame. But in the other side, everything works perfectly. Here is how I get the chil iFrame object in order to fire the postMessage function:

var iFrame = document.getElementById('Frame').contentWindow;

当我在控制台中打印它时,我得到以下信息:

When I print it int he console, I get the following:

Window {parent: Window, opener: null, top: Window, length: 0, frames: Window…}

当我按如下方式进入postMessage函数时:

When I proceed to the postMessage function as follows:

iFrame.postMessage("message", "http://contoso.com");

加载页面时显示错误: iFrame.postMessage不是功能
当我在控制台中执行postMessage时,我得到 未定义

It shows me an error when loading the page: iFrame.postMessage is not a function. When I execute the postMessage in console, I get an undefined

我做错了什么?

推荐答案

试试这个

var iFrame = document.getElementById('Frame');
iFrame.contentWindow.postMessage("message", "http://contoso.com");

我也有这个问题。我从这个网站找到了解决方案 https://www.viget .com / articles / using-javascript-postmessage-to-talk-to-iframes

I had this problem too. I found solution from this website https://www.viget.com/articles/using-javascript-postmessage-to-talk-to-iframes

这篇关于发出与postMessage从父级到子级iFrame的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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