postMessage的文檔意味着跨域消息傳遞是可能的。但是:我該如何做跨域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');
全部無濟於事。
幫助?
總之,我是個白癡。用'window'代替'document',通過'window.opener.postMessage'完成回調。謝謝! – 2010-07-26 07:45:40
適應我們最好的:) – 2010-07-26 07:47:59