window.parent only works with iframes and window.opener works with separate windows.

On top of that, since I was trying to send a message to the parent window, but the new window was opened by the iframe IN that window.

So window.opener reached the iframe - then I had to actually send the message to its parent:

window.opener.parent.postMessage(etc, etc);
Answer from DAB on Stack Overflow
🌐
UsefulAngle
usefulangle.com › post › 4 › javascript-communication-parent-child-window
How to Communicate Between Parent and Child Windows in Javascript
// variable that holds the handle of the child let child_window_handle = null; // on opening child window document.querySelector("#open-child-window").addEventListener('click', function() { child_window_handle = window.open('child.php', '_blank'); ...
🌐
GitHub
gist.github.com › mbajur › 8325540
Working example of window.postMessage used for sending data from popup to parent page (works in IE) · GitHub
I came across this when searching for getting post message to work cross domains using a popup in IE11. The solution in this code didn't work for me in IE11, but a very similar solution that I found on SO did: https://stackoverflow.com/a/36630058/2754718 · In case for whatever reason that link breaks in the future, here is what it said: Building on answer by tangle, I had success in IE11 [and emulated IE10 mode] using following snippet: var submitWindow = window.open("/", "processingWindow"); submitWindow.location.href = 'about:blank'; submitWindow.location.href = 'remotePage to comunicate with';
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › Window › postMessage
Window: postMessage() method - Web APIs - MDN Web Docs
Any script in a document in a window can request to let a message get dispatched to a document in another window whose window object it has obtained, by calling .postMessage() on that window object. Consequently, any event listener used to receive messages must first check the identity of the sender of the message, using the origin and possibly source properties.
🌐
Javascriptbit
javascriptbit.com › transfer-data-between-parent-window-and-popup-postmessage-api
Send data from parent window to child popup window - PostMessage API | JavaScriptBit
The only catch is, you'll need the child window reference in the parent. /* Step 1 : Open popup */ const popup = window.open("https://google.com", "_blank"); /* Step 2 : Add message event listener in popup window Open the console of the newly opened window and add this code */ window.addEventListener("message", function(event) { if (typeof(event.data) === "string") { alert("Hello from the other side! " + event.data); } }); /* Step 3 : Send a message to popup */ const message = "Hello there!" popup.postMessage(message, "*");
Top answer
1 of 3
6

The following works for me in chrome, firefox, ie(didn't test more browsers)

assume 3 documents

  1. (www.mydomain.com/parent.html)the page that contains the 'main'-document with the link
  2. (bills.mydomain.com/child.html)the page that will be opened by the link
  3. (www.mydomain.com/dispatcher.html)explained later

at first set the domain-property of all 3 documents to mydomain.com

<script>
document.domain="mydomain.com";
</script>

in parent.html create a hidden iframe with a name-property of e.g. "hiddenframe". Also create some function that may later receive a response.

parent.html should now look like this:

<script>
document.domain="mydomain.com";
function fx(msg)//receives the response
{
  alert(msg)
}
</script>
<iframe name="hiddenframe" style="display:none"></iframe>
<a href="http://bills.mydomain.com/child.html" target="_blank">click</a>

In child.html you'll now be able to load a document into the hidden iframe inside parent.html

<script>
document.domain="mydomain.com";
window.open('http://www.mydomain.com/dispatcher.html','hiddenframe');
</script>

(don't be confused in face of the use of window.open() here, there will not open a new window, the page will be loaded into the iframe in parent.html)


In dispatcher.html you now may call the function inside parent.html

<script>
document.domain="mydomain.com";
parent.fx('you just got some response');
</script>

When you only need to reload the parent.html it's a little bit easier.

Again set the document.domain-property in parent.html and child.html(you don't need the iframe in parent.html and the dispatcher.html)

In parent.html also set the name-property of the window, e.g.

<script>
  window.name="parentTab";
</script>

In child.html you now may access the parentTab-window(tab)

<script>
    document.domain="mydomain.com";
    window.open('http://www.mydomain.com/parent.html','parentTab');
</script>

...or simply use "parentTarget" as target-property of a link or form in child.html

2 of 3
0

What I did for myself, I implemeted some ajax to submit changes from the window2 into database. I implemeted JSON to pull new data from the database back to window1

🌐
JavaScript.info
javascript.info › tutorial › frames and windows
Cross-window communication
April 13, 2022 - <!doctype html> <html> <head> <meta charset="UTF-8"> </head> <body> <form id="form"> <input type="text" placeholder="Enter message" name="message"> <input type="submit" value="Click to send"> </form> <iframe src="iframe.html" id="iframe" style="display:block;height:60px"></iframe> <script> form.onsubmit = function() { iframe.contentWindow.postMessage(this.message.value, '*'); return false; }; </script> </body> </html> To call methods and access the content of another window, we should first have a reference to it. ... From the popup: window.opener – is a reference to the opener window from a popup. For iframes, we can access parent/children windows using:
🌐
Medium
medium.com › somos-pragma › an-alternative-for-sending-data-to-another-place-or-to-localstorage-postmessage-dc7e72e7ea11
Understanding postMessage for Secure Cross-Origin Communication | by Diego Caceres | Pragma | Medium
July 11, 2024 - // Open a new window const childWindow ... to the child window const message = 'Hello from the parent window!'; const targetOrigin = 'https://your-domain.com'; childWindow.postMessage(message, targetOrigin); });...
Find elsewhere
🌐
Medium
medium.com › @mrajaeim › understanding-window-postmessage-and-window-parent-postmessage-in-javascript-f09d4eac68ba
Mastering Cross-Window Communication in JavaScript: postMessage vs BroadcastChannel Explained | by Mohammad Rajaei Monfared | Medium
October 16, 2025 - In this example, when the “Send Message to Parent” button in child.html is clicked, it sends the message “Hello from Child Window!” to its parent window (origin1.html).
🌐
Javascriptbit
javascriptbit.com › transfer-data-between-parent-window-and-iframe-postmessage-api
Send data between parent window and child iframe - PostMessage API | JavaScriptBit
March 15, 2022 - You just need to use the PostMessage API to send data via the window.parent reference of the parent window. <script> window.addEventListener('message', function(event) { console.log("Message received from the child: " + event.data); // Message received from child }); </script>
🌐
Stack Overflow
stackoverflow.com › questions › 66237187 › window-open-postmessage-or-pass-data-to-parent-window-cross-domain
window.open postmessage or pass data to parent window cross domain
February 17, 2021 - window.opener.postMessage("message",'*'); on the popup window · setInterval(function(){ document.addEventListener('message', function(e) { alert('got (from ' + e.origin + '): ' + e.data); }, false); },1000); but it is now working window.top or window.parent is now working as well.
🌐
GitHub
gist.github.com › cirocosta › 9f730967347faf9efb0b
Sending messages from child iframe to parent webpage · GitHub
I believe you can try and use the main page to ask the user media access, and retrieve the IDs, then you can send the IDs to the iframe via a message and use it there. Of course you need to make sure the Iframe is also under SSL because otherwise it wouldn't work.. ... Works perfectly! I didn't know parent can be used as global variable, like this are.
Top answer
1 of 2
37

The window.opener object is what you're looking for, used it from within your popup like so to call the a function of the parent window:

window.opener.yourFunc() 
2 of 2
5

Here is a fun and easy demo that is heavily inspired by this answer to a similar question (but modified for my own purposes to help investigate the most difficult bug of my career).

Create 2 files (in the same directory) as follows:

parent.html

<button type="button" onclick="popup('popup.html', '', 800, 200);">Add My Card</button>
=&gt;
<span id="retrievedData">No data yet.</span>    
<script>
    function popup(url, title, width, height) {
        var left = (screen.width / 2) - (width / 2);
        var top = (screen.height / 2) - (height / 2);
        var options = '';    
        options += ',width=' + width;
        options += ',height=' + height;
        options += ',top=' + top;
        options += ',left=' + left;    
        return window.open(url, title, options);
    }

    function setData(data) {
        console.log(data);
        var strData = JSON.stringify(data);
        document.getElementById('retrievedData').innerHTML = strData;
        var requestBinUrl = 'http://requestb.in/18u87g81';
        window.location.href = requestBinUrl + '?data=' + strData;
    }
</script>

popup.html

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<form id="popupForm" name="f">    
    <select id="urlField" name="url">
        <option>
            http://date.jsontest.com/
        </option>
        <option>
            http://time.jsontest.com/
        </option>
        <option>
            http://md5.jsontest.com/?text=HereIsSomeStuff
        </option>    
    </select>
    <div><input type="submit" /></div>    
</form>
<script>
    $('#popupForm').submit(function(e) {
        e.preventDefault();
        var url = $('#urlField').val();
        console.log(url);
        $.ajax({
            url: url
        }).then(function(data) {
            console.log(JSON.stringify(data));
            window.opener.setData(data);
            window.close();
        });
    });    
</script>
🌐
David Walsh
davidwalsh.name › window-iframe
window.postMessage Tip: Child-To-Parent Communication
September 11, 2012 - ... //the iframe code setInterval(function() { parent.postMessage("Hello","http://davidwalsh.name"); },1000); //The parent code $(window).on('message', function(e){ console.log('parent received message!: ',e.data); });
🌐
Stack Overflow
stackoverflow.com › questions › 40444882 › postmessage-from-child-to-parent-window-in-safari-using-window-open
postMessage from child to parent window in Safari using window.open
November 6, 2016 - Have not tried safari. Not sure. By variable mean var w = window.open("/path/to/resource/"). w references opened window at parent window.
🌐
Plus2Net
plus2net.com › javascript_tutorial › window-child3.php
Passing of value from Child to parent window - JavaScript
February 5, 2000 - In parent window. <input type=text name='p_name' id=n1 size='8'> <input type=text name='p_name2' id=n2 size='8'> In child window · <html> <head> <script langauge="javascript"> function post_value(){ opener.document.f1.n1.value = document.frm.c_name.value; opener.document.f1.n2.value = document.frm.c_name2.value; self.close(); } </script> <title>(Type a title for your page here)</title> </head> <body > <form name="frm" method=post action=''> <table border=0 cellpadding=0 cellspacing=0 width=250> <tr><td align="center"> Your name<input type="text" name="c_name" size=12 value=test> <input type="text" name="c_name2" size=12 value=test2> <input type=button value='Submit' onclick="post_value();"> </td></tr> </table></form> Passing data from Parent to Child Window → ← Window object Reference Window refreshing Parent window from Child →
🌐
Dev-bay
dev-bay.com › iframe-and-parent-window-postmessage-communication
Iframe and parent window postMessage communication – Dev Bay – front-end tips
There will be postal address to send postcard, but there will be no mounted place to receive that. So, like in real life, your child must first inform you that his or her home has postbox, and you can send a postcard with message 🙂 ... I name that functionality the “windows hand-shake”. Ide idea is easy – CHILD window must inform a PARENT window when it is loaded, then PARENT can send data to CHILD.
🌐
Microsoft Learn
learn.microsoft.com › en-us › windows › win32 › inputmsg › wm-parentnotify
WM_PARENTNOTIFY message - Win32 apps | Microsoft Learn
Sent to a window when a significant action occurs on a descendant window. This message is now extended to include the WM_POINTERDOWN event. When the child window is being created, the system sends WM_PARENTNOTIFY just before the CreateWindow ...