【问题标题】:How can I send an event from parent window to its child window?如何将事件从父窗口发送到其子窗口?
【发布时间】:2018-03-27 15:10:31
【问题描述】:

我想将一些事件/数据从父窗口发送到其子窗口。我该怎么做?

我尝试过 postMessage,但没有成功。

【问题讨论】:

    标签: javascript html dom-events postmessage


    【解决方案1】:

    现在可以了,之前我犯了一些语法错误。这是相同的正确代码。

    //parent Window
    childWindow=window.open("http:/localhost/abcd.php","_blank","width=500,height=500");
    childWindow.postMessage('message',"http://localhost:80/child.php");
    
    
    //child Window
    var newUrl='';
    window.addEventListener(
     "message",
     function(e) { 
    
    console.log(e.data);//your data is captured in e.data 
    }, false);
    

    【讨论】:

      【解决方案2】:

      如果您所指的子窗口是 iFrame,那么 postMessage 就是您坚持使用的方法。

      没有看到您尝试过的任何代码,很难说您对 postMessage 的特定用法不起作用 - 但是,这里有一个指向 working postMessage example from an MDN article 和 @987654322 的链接@你可以从中剖析代码。

      其中一个应该能够向您展示为什么您的实施不起作用。

      【讨论】:

        猜你喜欢
        • 2012-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-07
        相关资源
        最近更新 更多