How to disconnect JavaScript popup from opener

后端 未结 2 552
生来不讨喜
生来不讨喜 2020-12-16 19:11

I\'m opening a popup from my main page with code like this:



        
相关标签:
2条回答
  • 2020-12-16 19:39

    If the page in the child window is in your control, you can assign null to the opener in the child page:

    window.opener = null;
    

    By making this as the first statement in your javascript.

    If the page is not in your control or is in a different domain, then do it while opening:

    popup = window.open(this.href, '_blank', 'width=512,height=512,left=200,top=100');
    popup.opener = null;
    
    0 讨论(0)
  • 2020-12-16 19:40

    From doc:

    In some browsers, a rel="noopener" attribute on the originating anchor tag will prevent the window.opener reference from being set.

    See all supported browsers: https://caniuse.com/#search=noopener

    Also, for older browsers use no-referrer: https://mathiasbynens.github.io/rel-noopener/

    So the fix is do rel="noreferrer noopener" wherever you are using target="_blank"

    0 讨论(0)
提交回复
热议问题