TAG
JavaScript
Result
Run
License
<button type="button" id="btn-openwin">Open new window</button>
window.addEventListener("DOMContentLoaded", () => { const elBtn = document.getElementById("btn-openwin"); let windowDialog; elBtn.addEventListener("click", e => { let left = (screen.width - 200) / 2; let top = (screen.height - 100) / 2; windowDialog = window.open('', '', `left=${left},top=${top},width=200,height=100`); let elTestBtn = document.createElement("button"); elTestBtn.textContent = "Send message to opener"; elTestBtn.addEventListener("click", sendMsgToOpener); windowDialog.document.body.append(elTestBtn); }); function sendMsgToOpener() { windowDialog.opener.postMessage(["Got it", "#FFFF00"]); } window.onmessage = e => { elBtn.textContent = e.data[0]; elBtn.style.backgroundColor = e.data[1]; windowDialog.close(); }; });
Console
expand_less
License
License
by DevDic
Close