收听来自iframe的消息

时间:2013-05-06 18:47:17

标签: javascript google-chrome-extension cross-domain postmessage

我已经阅读了一些文章,内容涉及window.postMessage如何用于发送消息,以及如何在页面和iframe之间发送消息。

我的问题是,我在接收通知所需的同一页面中有一个iframe。每隔一段时间iframe's DOM就会被修改为包含通知。我希望这些通知从iframe传播到页面本身。如何使用window.postMessage完成此操作?

以下是该方案:

<html>
<head>
    <title></title>
</head>
<body>

    ...need to hear events from iframe here...

    <div class="container">
        <iframe src="http://example.com">..(stuff happens in here)..</iframe>
    </div>

</body>
</html>

让我们假设我可以编辑iframe's代码来发送我想要的任何事件,自定义事件会像那样工作,还是由于跨源策略而无法传播?

0 个答案:

没有答案