【发布时间】:2012-07-31 13:34:33
【问题描述】:
我编写了一个内容脚本,可以将 iframe 注入任何网站(因此是不同的域)。
我需要父网站向子 iframe 发送一些信息,但是我找不到方法。
代码
var targetFrame = $('#myIframe')[0];
targetFrame.contentWindow.postMessage('the message', '*');
不知何故不起作用,我收到Cannot call method 'postMessage' of undefined 错误。
但是当我直接在 Chrome 的控制台中尝试相同的代码时,它就成功了。
不过,我可以毫无问题地从子级向父级发送 postMessage,但只需要一种让父级向子 iframe 发送消息的方法。
【问题讨论】:
标签: javascript html cross-browser postmessage