最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript: Add event listener on parent popup window - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 2

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);
}
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.

发布评论

评论列表(0)

  1. 暂无评论