我以http://127.0.0.1/1.html的形式打开1.htm
1.html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<iframe id="ifr" src="http://localhost/2.html" width="100%" height="300">
</iframe>
<script>
iframe=document.getElementById("ifr");
iframe.contentWindow.postMessage("hello there", "http://localhost");
</script>
</body>
</html>2.html
<!DOCTYPE html>
<html>
<head>
<script>
window.addEventListener("message", function(event) {
alert(hi);
if (event.data === "hello there" ) {
alert("Hi" + event.origin);
}
}, false );
</script>
<head>
<body>
Hello world
</body>“
but I have that error: "Unable to post message to http://localhost. Recipient has origin http://127.0.0.1/ 这是一个简单的例子。最后,我需要这样的结构:在域"A“上,我有iframe,它的src是域"B”的页面。如果是的话,里面有按钮。当我点击显示在iframe中的那个按钮时,我需要调用域的window.addEventListener "A“,我该怎么做呢?
发布于 2013-01-25 17:21:48
正如所描述的here。你只有以下选择。
相同或不同领域场景之间的通信:
+-------------------------+-----------+-------------+-------------+
| | home.html | framed.html | helper.html |
+-------------------------+-----------+-------------+-------------+
| www.foo.com/home.html | N/A | YES | YES |
| www.bar.net/framed.html | NO | N/A | YES |
| www.foo.com/helper.html | YES | YES | N/A |
+-------------------------+-----------+-------------+-------------+这纯粹是对CSRF (跨站点请求伪造)攻击的浏览器限制。看看你的简历,就连你也在同一领域工作。一种选择是遵循上面的层次结构示例,您可以在页面之间传递消息,甚至可以使用父站点上的助手页在相同或不同的域中传递消息。然后,孩子可以通过这个帮助页面接收和发送消息。
发布于 2013-01-25 17:15:27
检查您的主机文件(C:\Windows\System32 32\drivers\etc),确保您没有将localhost映射为其他任何内容。
注意,通过Javascript访问父帧不是这样的,因为这会带来安全问题(跨站点脚本)。
发布于 2013-01-30 10:38:54
解决办法如下:
<iframe id="frameId" src="http://b.net/2.html" onload="sendCommand();"> No Frame!</iframe>
<script type="text/javascript">
function sendCommand() {
var receiver;
receiver = document.getElementById('frameId').contentWindow;
receiver.postMessage(receiver, 'http://b.net');
}
</script>https://stackoverflow.com/questions/14526542
复制相似问题