Reputation: 3308
i'm currently trying to add an event listener on a popup window that is opened in a parent page.
Let me give you a little bit more explanation: There is a button in an iframe. When this button is clicked an popup window is opened in the parent page that is calling/holding the iframe.
Here is my code:
parent.window.open(url, "MyParentWindowPopUp", "width=1000, height=800");
With this line of code i open the popup window in the parent page.
When the windows is opened i have to create a listener on it so here is what i use when i add a listener for NON-parent window:
if (window.addEventListener) {
window.addEventListener('message', <?php echo $_htmlId; ?>_receive_message, false);
} else if (window.attachEvent) {
window.attachEvent('onmessage', <?php echo $_htmlId; ?>_receive_message);
}
How can i transform this code to work for the parent popup window?
Thanks in advance!
Upvotes: 3
Views: 12208
Reputation: 6770
You can store the reference to the opened Window
var popupWindow = parent.window.open(url, "MyParentWindowPopUp", "width=1000, height=800");
if (popupWindow.addEventListener) {
popupWindow.addEventListener('message', <?php echo $_htmlId; ?>_receive_message, false);
} else if (popupWindow.attachEvent) {
popupWindow.attachEvent('onmessage', <?php echo $_htmlId; ?>_receive_message);
}
Upvotes: 2
Reputation: 57729
var popup = parent.window.open(/**/);
popup.addEventListener("message", function () {
console.log("popup window received message");
});
popup.postMessage("foo");
.open
gives you a reference to the window.
Upvotes: 0