admin管理员组文章数量:1391951
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.
本文标签: JavaScript Add event listener on parent popup windowStack Overflow
版权声明:本文标题:JavaScript: Add event listener on parent popup window - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744678711a2619267.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论