Javascript 在 IE9 上,Win 7 window.open() 返回 null 而不是打开窗口的引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7014242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:15:41  来源:igfitidea点击:

On IE9, Win 7 window.open() returns null instead of reference of the opened window

javascriptbrowserinternet-explorer-9

提问by srayker

I am trying something like below,

我正在尝试类似下面的东西,

var myWindow = null;
if(!myWindow || myWindow.closed)
{
    myWindow = window.open(url, windowId);
}
else 
{
    myWindow.focus();   
}

All browsers return the window ref for window.open(), but IE 9 returns NULL. Does anybody have more information on why null is returned or possibly a workaround solution for IE9?

所有浏览器都返回 window.open() 的窗口引用,但 IE 9 返回 NULL。有没有人有更多关于为什么返回 null 的信息或可能是 IE9 的变通解决方案?

回答by srayker

window.openreturns a NULL reference object if Enable Protected Modeis checked under Internet Options->Security->Security Level for this zoneand the ZONE is different i.e. in my case local file opening a popup from Intranet.

window.open如果在Internet 选项-> 安全性-> 安全级别下选中启用保护模式此区域并且该区域不同,即在我的情况下本地文件打开来自 Intranet 的弹出窗口,则返回 NULL 引用对象。

window.openreturns a reference object even if Enable Protected Modeis checked when mysite.com opens someothersite.com in popup window i.e. Internet->Internet

window.open即使当 mysite.com 在弹出窗口(即 Internet->Internet)中打开 someothersite.com 时选中启用保护模式,也会返回参考对象

回答by OverZealous

Out of curiosity, what's your windowId? Does it have a space in it?

出于好奇,你的windowId呢?它里面有空间吗?

Because, apparently IE9 will fail if there's a space in the window title.

因为,如果窗口标题中有空格,IE9 显然会失败

回答by GB Patil

Actually in my case, I was getting window.open reference null if I'm opening localhost or intranet site URL. If my popup contains any internet site url then I'm getting window object in reference. I did following to resolve this:

实际上,在我的情况下,如果我打开 localhost 或 Intranet 站点 URL,我会得到 window.open 引用 null。如果我的弹出窗口包含任何 Internet 站点 url,那么我将引用 window 对象。我做了以下来解决这个问题:

  • Open IE > Tools > Internet Options
  • Go to Security Tab
  • Enable Protected Mode by clicking the checkbox
  • Restart IE
  • 打开 IE > 工具 > Internet 选项
  • 转到安全选项卡
  • 通过单击复选框启用保护模式
  • 重启浏览器

Now, it started giving me window object reference for popup window opened with Intranet Sites.

现在,它开始为我提供使用 Intranet 站点打开的弹出窗口的窗口对象引用。

Also, it could be the URL problem where it is secure or added in trusted sites zone. If you can provide the URL it will help.

此外,它可能是安全的或添加到受信任站点区域中的 URL 问题。如果您可以提供 URL,它将有所帮助。

Hope this helps.

希望这可以帮助。

回答by brightDot

We had this problem with our app. We are using external uploads using Dropbox and it was not working.

我们的应用程序遇到了这个问题。我们正在使用 Dropbox 使用外部上传,但它不起作用。

This is how we fixed it. Summarizing the different reads and solutions. Please let me know if any part is not correct or does not make sense.

这就是我们修复它的方式。总结不同的读取和解决方案。如果任何部分不正确或没有意义,请告诉我。

Root Cause:

Root Cause:

Starting from IE11, Windows has started assigning the security levels to the applications at the time of startup. Low-Security window and High-security window. Also starting IE11, when new popup opens, it is not a sub-window of the main browser window. Instead, it is a whole new IE11 instance with different security level assigned to it. Usually, the external upload mechanism works using the cross window messaging feature of JavaScript. If that does not work, external uploads do not work. As per windows security standards, a low-security window cannot send a message to the high-security window. Taking a step back and looking at our windows machine, they are shipped by our Internal team. By the default, all our domain web applications are added to trusted sites in our windows machines. But external upload links like Dropbox is not added to the trusted sites. So when you open Our App in IE11, windows automatically marks IE11 window as High-security window. Now, when you try to upload a design file through Dropbox because Dropbox is not trusted site, the new popup that gets created is a new IE11 instance with low-security level. Due to this, after the Dropbox window opened, it loses connection with the parent window that is Our App. This is causing the issue with External uploads

从 IE11 开始,Windows 开始在启动时为应用程序分配安全级别。低安全性窗口和高安全性窗口。同样启动 IE11,当新弹出窗口打开时,它不是主浏览器窗口的子窗口。相反,它是一个全新的 IE11 实例,分配了不同的安全级别。通常,外部上传机制使用 JavaScript 的跨窗口消息传递功能工作。如果这不起作用,则外部上传不起作用。根据 Windows 安全标准,低安全性窗口不能向高安全性窗口发送消息。退后一步看看我们的 Windows 机器,它们是由我们的内部团队运送的。默认情况下,我们所有的域 Web 应用程序都被添加到我们 Windows 机器中的受信任站点。但是像 Dropbox 这样的外部上传链接不会添加到受信任的站点。所以当你在 IE11 中打开 Our App 时,windows 会自动将 IE11 窗口标记为高安全性窗口。现在,当您尝试通过 Dropbox 上传设计文件时,因为 Dropbox 不是受信任的站点,创建的新弹出窗口是一个新的低安全级别的 IE11 实例。因此,在 Dropbox 窗口打开后,它与我们的应用程序的父窗口失去了连接。这导致了外部上传的问题 它失去了与作为我们的应用程序的父窗口的连接。这导致了外部上传的问题 它失去了与作为我们的应用程序的父窗口的连接。这导致了外部上传的问题

Fix:

Fix

  1. Either add both yoursite.com and dropbox.com to trusted sites in IE11 through Internet Options > Security > Trusted Sites or remove them both.

  2. Enable Protected Mode in IE11 by marking Enable Protected Mode in IE11 through Internet Options > Security > Enable Protected Mode and then restart the browser.

  1. 通过 Internet 选项 > 安全 > 受信任站点将 yoursite.com 和 dropbox.com 添加到 IE11 中的受信任站点,或者将它们都删除。

  2. 通过 Internet Options > Security > Enable Protected Mode 标记 Enable Protected Mode in IE11 在 IE11 中启用 Protected Mode,然后重新启动浏览器。

This means it is not a real issue for our Customers. If it's not working for them, it is either due to the reason, either of the sites is added to the trusted sites list but not both.

这意味着这对我们的客户来说不是真正的问题。如果它对他们不起作用,则可能是由于原因,其中一个站点已添加到受信任站点列表中,但不会同时添加到受信任站点列表中。