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!
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!
Share Improve this question asked Feb 10, 2015 at 16:50 VenelinVenelin 3,3067 gold badges71 silver badges149 bronze badges2 Answers
Reset to default 2You 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);
}
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.