4

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.

Ulhas Shetty
  • 51
  • 1
  • 7
  • What's the actual error message (if there is any)? `tab = window.open(...); tab.close()` works fine for me in IE11. – Christoph Feb 05 '19 at 21:41
  • No specific error reported. The tab just doesnt close with window.close invoked using assigned (window.open()) variable. No errors in the console log either. 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. I have added an update note in the question as well, for the same. – Ulhas Shetty Feb 05 '19 at 21:47
  • Your code looks a bit messy with all those global variables. Where do you actually initialize `win1` and `form`? I suppose `win1` might get re-assigned somewhere in your code. I encourage you to do proper variable initialization and hand them properly to the functions. – Christoph Feb 05 '19 at 21:48
  • win1 is initialized globally, after the – Ulhas Shetty Feb 05 '19 at 21:56
  • I'm surprised your code works at all in Chrome. It looks to me that with `form.submit();` you are reloading the page, thus destroying the value of your `win1`. It's a bit hard to tell since your example is not self contained. – Christoph Feb 05 '19 at 22:14
  • But it does seem to be working in Chrome as expected, and seems to be able to create a window object, in both Chrome and IE, when checked in the alert() logs. form.submit() is required to make the call a GET method, without which, and this is due to app server compatibility, i get the following error - "HTTP method POST is not supported by this URL". This "method POST not supported" is the error i am getting when i try window.open() without a form.submit() – Ulhas Shetty Feb 05 '19 at 23:42
  • I try to make a test with your above posted code and I find that it opens two tabs with the URL. IE and Chrome gave me same result. Can you please check your above code whether it has this issue or not. I suggest you to post working sample example may help us to understand the issue in better way. – Deepak-MSFT Feb 06 '19 at 09:29
  • Deepak, i have added the HTML code as well to get more clarity on where these functions are getting invoked from. And yes, i retried but the code which opens up another tab using window.open() seems to be closing the tab on Chrome but failing to close it for IE 11. – Ulhas Shetty Feb 06 '19 at 17:52
  • Maybe it depends on your IE browser settings. – Batsheva Hansav Feb 06 '19 at 20:17
  • Richard, tested my code across multiple machines but same error on all. Is there any specific IE browser setting that you are aware of which might need to be toggled for this to take effect? Had tried to disable third party add-ons on the browser i was testing in, but could not get it to work. – Ulhas Shetty Feb 06 '19 at 22:59
  • It may related to the event bubling that when I check your close html code you have two onclick event in each other, one on the command button which is return false, and the other one tied to the button's parent div which is close the opened window. Could you try to move the parent click event on the command link? – Seyhmus Gokcen Feb 06 '19 at 23:16
  • Did you find solution? – Batsheva Hansav Feb 09 '19 at 21:43
  • @Ulhas Shetty, Did you try to make a test with the suggestion given by Seyhmus. his example closing the window properly, If you did not tested it or if your issue is still persist than try to inform the current status of your issue. We will again try to provide further suggestions. Thanks for your understanding. – Deepak-MSFT Feb 14 '19 at 07:02

2 Answers2

2

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.

James_F
  • 449
  • 2
  • 8
  • It is partially right, and it is related to the ie security. If you change the window newly opened windows url as the same domain or give it blank you will get the window object. – Seyhmus Gokcen Feb 07 '19 at 00:46
0

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.

Seyhmus Gokcen
  • 248
  • 4
  • 10