简体   繁体   English

带有window.opener的IE中的CustomEvent

[英]CustomEvent in IE with window.opener

I have a problem since few days. 几天以来我有问题。

On firefox my code works but not on IE. 在Firefox上,我的代码有效,但在IE上却无效。 I have a window which open new window with window.open; 我有一个窗口,使用window.open打开新窗口; In this new window, I do what I want and after that I would like to update a specific part on parent window. 在这个新窗口中,我想要做的事情之后,我想更新父窗口中的特定部分。 On parent window , I had : 父窗口上 ,我有:

$(document).on('myEvent', doThis);

And on the second window I had this (I don't want to use other library like jQuery if is possible) : 第二个窗口中,我有这个(如果可能的话,我不想使用其他库,例如jQuery):

var event = new CustomEvent("myEvent");
window.opener.document.dispatchEvent(event);
window.close();

On Firefox the code seems Okay but on IE it doesn't work; 在Firefox上,代码看起来还可以,但是在IE上却无法正常工作; I've tried to add a CustomEvent polyfill (because I undestand IE doesn't implement CustomEvent), but I have new problem... IE doesn't like my : 我尝试添加一个CustomEvent polyfill(因为我不了解IE无法实现CustomEvent),但是我遇到了新问题... IE不喜欢我的:

window.opener.document.dispatchEvent(event);

How can I send an event on my "opener" (or parent) window when I finnish to do what I want in new window which was opened by my "opener" (or parent) window ? 当我完成在由“打开程序”(或父级)窗口打开的新窗口中执行所需的操作时,如何在“打开程序”(或父级)窗口上发送事件?

Thx. 谢谢。

Trigger the event from the parent with jQuery 使用jQuery触发父事件

var o = window.opener; 
o.$(o.document).trigger("myEvent");

I use hashchange event as a workaround since IE11 doesn't fire event to window.opener typically. 我将hashchange事件用作解决方法,因为IE11通常不会将事件触发到window.opener。

window.opener.window.location.hash = (new Date()).getTime().toString() + '&myEvent=true';
window.close();

Opener side 开瓶器侧

window.addEventListener('hashchange', function(){
    if (window.location.hash.indexOf('&myEvent=true') > -1) {
        // fire event on opener
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM