postMessage的文档暗示跨域消息传递是可能的。然而:
// When the popup has fully loaded, if not blocked by a popup blocker
这不是一个非常清楚的说明如何实际做到这一点。
想象两个网站:
- [家长] 托管在
qc-a.nfshost.com
- [儿童] 托管在
qc-b.quadhome.com
在父级中:
document.addEventListener('message', function(e) {
alert('Parent got (from ' + e.origin + '): ' + e.data);
e.source.postMessage('Round-tripped!', 'http://qc-b.quadhome.com');
}, false);
function go() {
var w = window.open('http://qc-b.quadhome.com', 'test');
/* This doesn't work because same-origin policy prevents knowing when
the opened window is ready. */
w.postMessage('Vain attempt.', 'http://qc-b.quadhome.com');
}
而且,在孩子身上:
document.addEventListener('message', function(e) {
alert('Child got (from ' + e.origin + '): ' + e.data);
}, false);
window.opener.postMessage('Ready!', 'http://qc-a.nfshost.com');
一切都无济于事。
帮助?