8

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?

3
  • Cant u try using cookies?? Apr 2, 2014 at 4:15
  • 2
    @AmarnathRShenoy - What's the fun of using cookies here. I have just simple requirement of catching a bool variable from child so that I can do my further taks on the parent window.
    – user1556433
    Apr 2, 2014 at 4:18
  • 1
    Take a look at stackoverflow.com/questions/12419361/…
    – LCJ
    May 11, 2014 at 15:56

3 Answers 3

19

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);
}
4
  • 1
    @Ashan shah - This is not working. The statements below window.open statement are executed as soon as the child window opens.
    – user1556433
    Apr 2, 2014 at 4:33
  • check modified. you can use set value to implement your logic
    – Ahsan Shah
    Apr 2, 2014 at 4:42
  • @Ashan Shah - yes, it is right now. I already implemented this. Thanks for your answer.
    – user1556433
    Apr 2, 2014 at 4:45
  • Effective solution Nov 16, 2021 at 15:56
2

Use below code and replace test with your element Id.

var parentDoc = window.opener.document;
parentDoc.getElementById("test").value = document.getElementById("test").value;
1
  • Hello Abhay, I tried your code but it's not working. I am getting error like this Cannot read properties of null (reading 'document')
    – Maximious
    Jan 11, 2022 at 6:29
0

You can use window.opener.postMessage():

in parent:

window.onmessage = function(m) {

  if(m.data) {
    // logic if 'OK' in child clicked
  }

}

in child:

function OKClicked() {
  window.opener.postMessage(true, '*');
  window.close(); 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy