Ulhas Shetty
Ulhas Shetty

Reputation: 51

window.close not working for opened window in IE 11

I am having a problem in IE 11 that when I open a new window via win1 = window.open(...) I am not able to close it again via win1.close().

Below, on logging out from the Main window, the child window opened under OpenWindowWithGet() does not get closed under IE 11. Works perfectly fine with Google Chrome.

[Update] : When I perform this operation on IE 11's Console under developer tools, I am able to open the window using window.open (using assigned variable) and close it using window.close() [as var1.close()]. Just something more than might help analyse this anomaly.

<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();

}


HTML code for OpenWindowWithGet() inside the MyManual() function -

                   <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>

HTML code for OAPLogout() -

                                <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>

Related question, but the provided resolution didnt help. questions/710756

Any suggestions?

Thank you.

Upvotes: 4

Views: 6743

Answers (2)

Seyhmus Gokcen
Seyhmus Gokcen

Reputation: 248

If your urllink is point to the another domain you will not get the window object on IE for the reason of the security (I may wrong, but I guess it is related to XSS issue).

If not; try the below code for demonstration;

<!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>

in the open line the 1st param must be the same domain or give it blank than you will be enable get the opened window to close it.

Upvotes: 0

James_F
James_F

Reputation: 449

I have done some testing and the answer I have come up with is: it's not your fault.

The problem does not revolve around win1.close, it's win1 being undefined because IE is special. If you run this code in IE 11:

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

it logs: null. This is because for some reason, window.open returns null rather than a reference to the new window. This SO question has solution to your problem. The most upvoted answer says that opening a window from your website hosted from a local file enables this behavior, if you host your website then it should behave like it does on other browsers.

Upvotes: 2

Related Questions