JavaScript window.open(): Open a Window and Pass Values Both Ways

Call window.open() in a click handler, then use the returned window reference and postMessage() to exchange values. The child in this example announces when it is ready. Each receiver checks the origin, the sending window, and the data before acting on a message.

Open a window or tab

In window.open(url, target, features), target is a reusable window name or a keyword such as _blank. The feature string popup,width=420,height=320 requests a small popup. Browser settings may open a tab or change its dimensions. Call the method directly from a click handler. If a call like the one below returns null without the noopener feature, handle the failure to open the window. HTML Standard · window.open()

Check where the message came from

An origin is the combination of protocol, host, and port. For example, http://localhost:8000 and http://127.0.0.1:8000 are different origins. Windows from different origins cannot access each other’s DOM directly. Use postMessage(data, targetOrigin) and specify the recipient’s exact origin. Check event.origin, event.source, the message type, and the value’s data type when receiving it. HTML Standard · postMessage()

Exchange values with two files

Save the two listings as parent.html and child.html in one folder. With Python 3 installed, run the command below in that folder and open http://127.0.0.1:8000/parent.html. Serving both files from this address gives them the same location.origin.

python -m http.server 8000 --bind 127.0.0.1

parent.html — When the parent receives ready, it sends the value currently in its input as an initial message.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Parent window</title>
</head>
<body>
<label for="value">Parent value</label>
<input id="value" value="Initial value">
<button id="open" type="button">Open child window</button>
<p id="status" role="status"></p>
<script>
const input = document.querySelector('#value');
const status = document.querySelector('#status');
const origin = location.origin;
let child = null;

document.querySelector('#open').addEventListener('click', () => {
  child = window.open('child.html', 'valueExample', 'popup,width=420,height=320');
  status.textContent = child ? "Waiting for the child window to respond." : "Could not open the popup.";
});

window.addEventListener('message', event => {
  if (!child || event.origin !== origin || event.source !== child) return;

  const data = event.data;
  if (data?.type === 'ready') {
    child.postMessage({ type: 'initial', value: input.value }, origin);
  } else if (data?.type === 'result' && typeof data.value === 'string') {
    input.value = data.value;
    status.textContent = "Received a value from the child window.";
  }
});
</script>
</body>
</html>

child.html — The child enables the send button after it receives the initial value. Clicking the button returns the edited string in a result message.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Child window</title>
</head>
<body>
<label for="child-value">Child value</label>
<input id="child-value">
<button id="send" type="button" disabled>Send to parent window</button>
<p id="child-status" role="status"></p>
<script>
const input = document.querySelector('#child-value');
const send = document.querySelector('#send');
const status = document.querySelector('#child-status');
const origin = location.origin;
const openerWindow = window.opener;

if (!openerWindow) {
  status.textContent = "Open this page with the parent window’s button.";
} else {
  window.addEventListener('message', event => {
    if (event.origin !== origin || event.source !== openerWindow) return;

    const data = event.data;
    if (data?.type !== 'initial' || typeof data.value !== 'string') return;
    input.value = data.value;
    status.textContent = "Received the parent window’s value.";
    send.disabled = false;
  });

  openerWindow.postMessage({ type: 'ready' }, origin);
}

send.addEventListener('click', () => {
  if (send.disabled) return;
  if (openerWindow.closed) {
    send.disabled = true;
    status.textContent = "The parent window is closed.";
    return;
  }
  openerWindow.postMessage({ type: 'result', value: input.value }, origin);
  status.textContent = "Sent the value to the parent window.";
});
</script>
</body>
</html>

Enter 123 in the parent and open the child. The child input should show 123. Change it to 456 and send it back. The parent input should now show 456. To end the exercise, close the child window and press Ctrl+C in the server terminal.

Troubleshoot missing values

If nothing opens, check your browser’s popup settings and try the button again. If the child opens with its send button disabled, check that both pages share an origin and that you opened the child from the parent’s button. Sending after the parent closes produces a message in the child window.

The example keeps the opener connection for its two cooperating documents. For an external page that needs no messaging, use rel="noopener" on a link or the noopener feature in window.open() to cut the connection. Opening a tab successfully with noopener can still return null. A site’s document isolation policy can also sever the opener connection.