简体   繁体   English

由 window 打开的 Window.open 不会关闭

[英]Window that was opened by window.open won't close

I'm having problems with a piece of code that has worked before for years, but seems to have stopped working now.我遇到了一段以前工作多年的代码问题,但现在似乎已经停止工作了。

I'm opening a window with a login form and I'm listening via a WebSocket for events regarding that login.我正在打开带有登录表单的 window,我正在通过 WebSocket 收听有关该登录的事件。 After the login was successful, I want to close the window (that my script has opened and kept the reference to) after a short moment.登录成功后,我想在片刻后关闭 window(我的脚本已打开并保留对它的引用)。 I'm using the following code:我正在使用以下代码:

const windowManager = {
  window: null,
  eventType: null,
}
function openWindow({ url, eventType }) {
  windowManager.window = window.open(url)
  windowManager.eventType = eventType
}
function closeWindow({ eventType }) {
  if (windowManager.window && windowManager.eventType == eventType) {
    setTimeout(() => {
      windowManager.window && windowManager.window.close()
      windowManager.window = null
    }, 100)
  }
}

I have confirmed that windowManager.window.close() is called and does not thrown an error.我已经确认windowManager.window.close()被调用并且没有抛出错误。 I have also extracted the code from the application and tested it separately and it still won't close the window. As I said, this piece of code has worked before and was not changed in the past two years or so.我也从应用程序中提取代码并单独测试它仍然不会关闭window。正如我所说,这段代码在过去两年左右没有改变。

I'm using the following browsers:我正在使用以下浏览器:

  • Safari 15.3 Safari 15.3
  • Firefox 97.0b9 (Developer Edition) Firefox 97.0b9(开发版)
  • Chromium 94.0.4606.61铬 94.0.4606.61

I'm grateful for any pointers which could help resolve this issue.我很感激任何可以帮助解决这个问题的建议。 Thanks a lot!非常感谢!

After figuring out that the above code worked totally fine with other sites like Google or GitHub, I found that the Cross-Origin-Opener-Policy header in our auth backend (which was the site that was opened with the code) is the culprit.在弄清楚上面的代码与其他站点(如 Google 或 GitHub)完全正常工作后,我发现我们的身份验证后端(这是使用代码打开的站点)中的Cross-Origin-Opener-Policy header 是罪魁祸首。 We had just updated Helmet to version 5 which added the header by default.我们刚刚将Helmet更新到版本 5,默认情况下添加了 header。

Our solution was to set Cross-Origin-Opener-Policy to same-origin-allow-popups on both source and target window (which are hosted on the same origin, but served by different servers).我们的解决方案是在源和目标 window 上将Cross-Origin-Opener-Policy设置为same-origin-allow-popups (它们托管在同一来源,但由不同的服务器提供服务)。 It also worked when setting it to unsafe-none for the target window without setting it at all on the source window.在将目标 window 设置为unsafe-none而未在源 window 上设置它时,它也有效。

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

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