我敢肯定这只是我的语法问题,但我试图发送一个变量到iframe(用于colorbox)。目前我接受任何两端的域名(只是为了让它工作)。下面是发送页面的JS:使用postMessage()没有收到
$(document).ready(function() {
if(window.location.hash) {
var urlimage = window.location.hash.substring(1);
targetiframe = document.getElementById('wbgallery').contentWindow;
targetiframe.postMessage(urlimage, "*");
console.log(urlimage);
}
});
这里是接收页:
$(document).ready(function() {
window.addEventListener('message',receiveMessage);
console.log(event);
function receiveMessage(event) {
if (origin !== "*")
return;
inbound = event.data;
console.log(inbound);
}
});
我看到控制台日志urlimage,可以看到入站事件,但没有。我使用Mozilla's explanation来尝试和全力以赴。
'receiveMessage'是使用从未设置变量'origin'。所以它返回。 – Barmar
您还在'event'参数的函数外部有'console.log(event)'。那应该做什么? – Barmar
我想'origin'应该是'event.origin',但你为什么还要打扰呢?它永远不可能是'*',它总是一个URL。 – Barmar