Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何进行跨域postMessage?_Javascript_Html_Cross Domain_Postmessage - Fatal编程技术网

Javascript 如何进行跨域postMessage?

Javascript 如何进行跨域postMessage?,javascript,html,cross-domain,postmessage,Javascript,Html,Cross Domain,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

的文档表明跨域消息传递是可能的。然而:

// 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');
    
    都没有用


    帮助?

    您正在打开窗口并在彼此之后发布消息。打开的文档不可能准备好接受post消息。尝试延迟postMessage调用,直到窗口完成加载


    测试这一点的一个非常简单的方法是在setTimeout(10秒)中包装w.postMessage(),看看它是否可以在文档准备好时发布它。

    目前,我看到两个问题。代码中有轻微错误和超时问题

    1) 我在代码中看到的错误是您使用的是document.addEventListener。我认为正确的是window.addEventListener。它在页面上的示例中


    2) 通过超时,您可以将子窗口的postMessage发送给父窗口。父窗口将知道子窗口何时准备就绪。

    正确。代码中的注释提到(我知道)无法知道窗口何时准备就绪。10秒的超时似乎有点。。。哈奇。注意,在child窗口中,我尝试做一个postMessage,指示准备就绪,返回到开瓶器。这也失败了。想法?简言之,我是个白痴。将
    文档
    替换为
    窗口
    ,并通过
    窗口.opener.postMessage
    执行就绪回调。非常感谢。