代码之家  ›  专栏  ›  技术社区  ›  Scott Robinson

如何跨域发送邮件?

  •  7
  • Scott Robinson  · 技术社区  · 16 年前

    postMessage 意味着跨域消息传递是可能的。然而:

    // When the popup has fully loaded, if not blocked by a popup blocker
    

    这不是一个很清楚的问题 怎样

    想象两个网站:

    1. [父]托管于 qc-a.nfshost.com
    2. [Child]主持时间 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');
    

    一切都无济于事。

    2 回复  |  直到 12 年前
        1
  •  8
  •   Anh-Kiet Ngo    16 年前

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

    postMessage 第页。

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

        2
  •  0
  •   Ben Rowe    16 年前

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

    推荐文章