Javascript 侦听来自iframe的消息
我读过一些关于如何使用Javascript 侦听来自iframe的消息,javascript,google-chrome-extension,cross-domain,postmessage,Javascript,Google Chrome Extension,Cross Domain,Postmessage,我读过一些关于如何使用window.postMessage在页面和iframe之间来回发送消息的文章 我的问题是,我在同一页面中有一个iframe,我需要接收来自该页面的通知。每隔一段时间,iframe的DOM就会被修改以包含通知。我希望这些通知从iframe传播到页面本身。如何使用window.postMessage 以下是场景: <html> <head> <title></title> </head> <body&g
window.postMessage
在页面和iframe
之间来回发送消息的文章
我的问题是,我在同一页面中有一个iframe
,我需要接收来自该页面的通知。每隔一段时间,iframe的
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的事件。。。
..(这里发生的事情)。。
假设我可以编辑iframe的代码来发送我想要的任何事件,自定义事件是这样工作的,还是由于跨源策略而无法传播?top.postMessage()会出现。这篇文章可能会有帮助:该死!谢谢你的提示,工作起来很有魅力:)