如何使用窗口将值从子窗口返回到父窗口。在Javascript中开放?

时间:2022-10-22 15:17:15

I have a parent window from where I am opening a child window using window.open like following. I want to get a boolean value from the child window on the basis of which I will do my tasks in parent window.

我有一个父窗口,我在这里打开一个使用窗口的子窗口。打开后。我希望从子窗口获得一个布尔值,在此基础上,我将在父窗口中执行任务。

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>

When the button is clicked I want to return true from here. But all this is not working for me. Am I lacking syntax here?

单击按钮时,我想从这里返回true。但这一切对我都不起作用。我这里缺少语法吗?

1 个解决方案

#1


10  

you can do it like this:

你可以这样做:

In parent:

家长:

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
}

in popup:

在弹出:

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

#1


10  

you can do it like this:

你可以这样做:

In parent:

家长:

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
}

in popup:

在弹出:

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