javascript postMessage无效 [英] javascript postMessage not working
问题描述
我不知道该怎么办。我尝试了不同来源的几个示例代码,我在不同的浏览器(从Chrome 9到FF 4)中尝试过它们,但似乎仍然没有使用postMessage功能。
JS控制台没有给我任何东西,没有一个错误,仍然没有发生任何事情:框架不想进行通信。完全没有。这甚至不是跨域的:两个帧都来自我的域。
I don't know what to do. I tried several sample codes from different sources, I tried them in different browsers (from Chrome 9 to FF 4), and still nothing seems to be working with the "postMessage" function. JS console is giving me nothing, not a single error, still nothing is happening : the frames don't want to communicate. At all. And this isn't even cross-domain : both frames are from my domain.
以下是上次尝试的示例代码:
父框架:
Here is a sample code from the last try : Parent frame :
<iframe src="IFRAME_URL"></iframe>
<script>
window.addEventListener( "message",
function (e) {
if(e.origin !== 'DOMAIN'){ return; }
alert(e.data);
},
false);
</script>
子框架:
<html>
<head></head>
<body>
<script>
top.postMessage('hello', 'DOMAIN');
</script>
</body>
非常感谢,谢谢很多
推荐答案
你的 postMessage
的第二个参数必须是一个url http:// localhost
The second parameter of your postMessage
must be an url like http://localhost
这篇关于javascript postMessage无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!