如何使用Javascript中的window.open从子窗口向父窗口返回值?

问题描述 投票:5回答:2

我有一个父窗口,使用window.open从那里打开子窗口,如下所示。我想从子窗口中获取一个布尔值,以此为基础,我将在父窗口中执行任务。

function openChildWin() 
{   
    var childWin = window.open("childWin.html", "_blank", "height=400, width=550, status=yes, toolbar=no, menubar=no, location=no,addressbar=no); 
    if (childWin)
    {
        //some logic
    }
    else
    {
        //some logic
    }
} 

**childWin.html**



 <!DOCTYPE html>
    <html lang="en">
      <head>

        <script type="text/javascript">

            function OKClicked()
            {
                //return true; //I want to return true here
                            window.close(); //Close this child window  
            }

        </script>

      </head>
      <body>
        <button type="button" id="OKButton" onclick="OKClicked()">Click ME</button>
      </body>
     </html>

单击按钮后,我想从此处返回true。但是,这一切都不适合我。我在这里缺少语法吗?

javascript jquery html dom
2个回答
11
投票

您可以这样做:

在父母中:

function openChildWin() {   
    var childWin = window.open("childWin.html", "_blank", "height=400, width=550, status=yes, toolbar=no, menubar=no, location=no,addressbar=no"); 
}
function setValue(val1) {
   // do your logic here
}

在弹出窗口中:

function OKClicked() {
    window.opener.setValue(true);
}

0
投票

使用下面的代码,并用您的元素ID替换测试。

var parentDoc = window.opener.document;
parentDoc.getElementById("test").value = document.getElementById("test").value;
© www.soinside.com 2019 - 2024. All rights reserved.