简体   繁体   English

如何在JavaScript中实现postMessage()?

[英]How do I implement postMessage() in JavaScript?

I need help with the window.postMessage method. 我需要有关window.postMessage方法的帮助。 I'm trying to post a message to an iframe from the parent window. 我正在尝试从父窗口将消息发布到iframe。

The iframe's name and ID is myFrame , and I have tried the following code, with no avail: iframe的名称和ID为myFrame ,并且我尝试了以下代码,但无济于事:

document.getElementById('myFrame').contentWindow.postMessage('Hello','*')

Take a step back and debug. 退后一步并进行调试。

  1. What does document.getElementById('myIframe') give you? document.getElementById('myIframe')给您什么?
  2. What does document.getElementById('myIframe').contentWindow give you? document.getElementById('myIframe').contentWindow给您什么?
  3. Do you have a global method named postMessage in the iframe? iframe中是否有一个名为postMessage的全局方法?
  4. Is the iframe domain the same as the initial page? iframe域与初始页面相同吗? It has to be. 它一定要是。
  5. Does the browser throw any errors in the console? 浏览器是否在控制台中引发任何错误?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM