How to return value from child window to parent window using window.open in Javascript?

I have a parent window, from where I open the child window using window.open, as shown below. I want to get a boolean value from a child window, based on which I will perform my tasks in the 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 pressed, I want to return true here. But all this does not work for me. Am I missing the syntax here?

+4
source share
1 answer

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 window:

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

Source: https://habr.com/ru/post/1534578/


All Articles