没有从iframe收到postMessage [英] postMessage not being received from iframe
本文介绍了没有从iframe收到postMessage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是iframe中的代码 src =example.com
Here is the code in the iframe with src="example.com"
<script>
var domain = "http://example2.com";
function redirectRequest(){
console.log("window.opener",window.opener); // NULL
console.log("window.top",window.top); // script_name
console.log("window.parent",window.parent); // script_name
opener.postMessage("redirect", domain); //fails because null
//top and parent also do not work BUT do not display errors
}
</script>
这是在 example2.com $ c $中运行的代码c>其中包含postMessage接收器(还包含iframe):
and here is the code running in example2.com
which contains the postMessage receiver (and also contains the iframe):
function message_listener(event) { //nothing is ever received...
console.log("event received",event);
var data = event.data;
console.log("data received",data);
}
if (window.addEventListener) {
window.addEventListener("message", message_listener);
} else {
// IE8
window.attachEvent("onmessage", message_listener);
}
知道什么可能会关闭吗?非常感谢...
Any idea what might be off? Thank you very much...
推荐答案
对于iframe,您需要将opener替换为parent。
替换此:
For iframe you need to replace opener to parent. replace this:
opener.postMessage("redirect", domain);
到此:
window.parent.postMessage("redirect", domain);
这篇关于没有从iframe收到postMessage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文