【问题标题】:window.close not working for opened window in IE 11window.close 不适用于 IE 11 中打开的窗口
【发布时间】:2019-02-05 21:30:38
【问题描述】:

我在 IE 11 中遇到问题,当我通过 win1 = window.open(...) 打开一个新窗口时,我无法通过 win1.close() 再次关闭它。

下面,从主窗口注销时,在 OpenWindowWithGet() 下打开的子窗口不会在 IE 11 下关闭。 与 Google Chrome 完美搭配。

[更新]:当我在开发人员工具下的 IE 11 控制台上执行此操作时,我可以使用 window.open(使用分配的变量)打开窗口并使用 window.close() [as var1.close ()]。 只是一些可能有助于分析这种异常的东西。

<html>
<script type="text/javascript">
var win1; 

function OpenWindowWithGet(urllink, windowoption, name){

var form = document.createElement("form");
form.method = "GET";
form.action = urllink;
form.target = name;

var input = document.createElement("input");
input.type = "hidden";
input.name = "OAP_Id";
input.value = OAP_Id;
form.appendChild(input);

document.body.appendChild(form);
win1 = window.open(urllink, "Manual", windowoption);
form.submit();}

function OAPLogoutClick(){ 
     disconnectFromNodeServer();
     win1.close();

}


MyManual() 函数中 OpenWindowWithGet() 的 HTML 代码 -

                   <h:panelGrid  id="manual" columns="2" cellpadding="2px" cellspacing="0" style="display: inline-block;">
                    <h:panelGroup>
                       <a onclick="return myManual();" target="_blank" style="width: 40px; display: inline-block;  position: relative; top: 2px; color: #000000">Manual</a>
                    </h:panelGroup>
                    <h:outputLabel id="manualSeparator" value=" | " style="color: #000000; display: inline-block;"/>
                </h:panelGrid>

OAPLogout() 的 HTML 代码 -

                                <div class="oapadding5" onclick="hideGroupedInfo();OAPLogoutClick();stopEventPropagation(event);"  onmouseover="onRowMouseOver(this);" onmouseout="onRowMouseOut(this);" style="display: #{oASession.m_bShowLogout eq 'true'? '':'none'}">
                                    <h:commandLink id="logoutLink2" styleClass="oatextpaddingleft" style="color: #{oAThemeBean.m_objCurrentThemeInfo.m_strTextFontColor};" value="#{OMNIGEN.LOGOUT}" onclick="return false;">
                                    </h:commandLink>
                                </div>

相关问题,但提供的解决方案没有帮助。 questions/710756

有什么建议吗?

谢谢。

【问题讨论】:

  • 实际的错误信息是什么(如果有的话)? tab = window.open(...); tab.close() 在 IE11 中对我来说很好。
  • 未报告具体错误。使用分配的 (window.open()) 变量调用 window.close 时,选项卡不会关闭。控制台日志中也没有错误。当我在开发人员工具下对 IE 11 的控制台执行此操作时,我可以使用 window.open(使用分配的变量)打开窗口并使用 window.close() [as var1.close()] 关闭它。只是一些可能有助于分析这种异常的东西。同样,我也在问题中添加了更新说明。
  • 你的代码看起来有点乱,所有这些全局变量。您实际上在哪里初始化win1form?我想win1 可能会在您的代码中的某个地方重新分配。我鼓励您进行适当的变量初始化并将它们正确地交给函数。
  • win1 被全局初始化,在
  • 我很惊讶您的代码完全可以在 Chrome 中运行。在我看来,使用form.submit();,您正在重新加载页面,从而破坏了您的win1 的价值。有点难以判断,因为您的示例不是自包含的。

标签: javascript html internet-explorer-11 window.open


【解决方案1】:

我做了一些测试,得出的答案是:这不是你的错。

问题不在于win1.close,而是win1 未定义,因为IE 很特殊。如果您在 IE 11 中运行此代码:

const win = window.open("https://www.google.com/")
console.log(win)

它记录:null。这是因为出于某种原因,window.open 返回null 而不是对新窗口的引用。 This SO question 可以解决您的问题。最受欢迎的答案是从本地文件托管的网站打开一个窗口会启用此行为,如果您托管您的网站,那么它应该像在其他浏览器上一样。

【讨论】:

  • 部分正确,和ie安全有关。如果您将新打开的窗口的 windows url 更改为相同的域或将其留空,您将获得窗口对象。
【解决方案2】:

如果您的 urllink 指向另一个域,出于安全原因,您将无法在 IE 上获取窗口对象(我可能错了,但我猜这与 XSS 问题有关)。

如果没有;试试下面的代码进行演示;

<!DOCTYPE html>
<html>
    <head></head>
    <body>
        <button onclick="openWin()">Open "myWindow"</button>
        <button onclick="closeWin()">Close "myWindow"</button>

        <script>
            var myWindow;

            function openWin() {
                var params = "width=200, height=100";
                myWindow = window.open("**here is have to be the same domain or leave it blank", "myWindow" /*, params */);
                console.log(myWindow);
                myWindow.document.write("<p>This is 'myWindow'</p>");
            }

            function closeWin() {
              myWindow.close();
            }
        </script>
    </body>
</html>

在打开的行中,第一个参数必须是相同的域或将其留空,否则您将启用打开的窗口以将其关闭。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-21
    • 1970-01-01
    • 2018-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多