有一些关于javascript postMessage事件的事件来源我没有得到的东西。
这是我的主页:
Test
Outside
width="100%" height="100"
sandbox="allow-scripts">
window.addEventListener('message', function (event) {
console.log(event);
}, false);
还有我的iFrame内容
Inside
var counter = 1,
domain = window.location.protocol + '//' + window.location.host,
send = function () {
window.setTimeout(function () {
console.log('iframe says:', domain);
window.parent.postMessage(counter, domain);
counter += 1;
send();
}, 3000);
};
send();
查看控制台,即使iFrame中的域变量正确,事件对象的origin属性也始终为null。
我的控制台说:
iframe-include.html:11 iframe says: http://127.0.0.1:8181
iframe.html:11 MessageEvent {isTrusted: true, data: 2, origin: "null", lastEventId: "", source: Window…}
在每个文档中,它都说在de“message”事件监听器中检查event.origin很重要。 但是如果它总是空的话怎么做呢?
谢谢您的帮助