0

I opened an external url (google.com) using windows.open(). Now the windows object (myWindows) is null, and hence I cant close the popup. Even if it is closed manually, I cant check through code if the popup has been closed.

One workaround is to uncheck the 'Enabled Protected Mode' in internet options. But that isn't a feasible solution. I suppose the issue is due to cross-origin restrictions in IE.

<!DOCTYPE html>
<html>
<head>
<script>

    var myWindow = {};

function openWin() {
    myWindow = window.open("http://www.google.com", "myWindow", "width=400, height=200");
}

function closeWin() {
    if (myWindow) {
        myWindow.close();
    }
}

function checkWin() {
    if (!myWindow) {
        document.getElementById("msg").innerHTML = "'myWindow' has never been opened!";
    } else {
        if (myWindow.closed) { 
            document.getElementById("msg").innerHTML = "'myWindow' has been closed!";
        } else {
            document.getElementById("msg").innerHTML = "'myWindow' has not been closed!";
        }
    }
}

</script>
</head>
<body>

<button onclick="openWin()">Open "myWindow"</button>
<button onclick="closeWin()">Close "myWindow"</button>
<br><br>
<button onclick="checkWin()">Has "myWindow" been closed?</button>
<br><br>
<div id="msg"></div>

</body>
</html>

The requirement is to open a 3rd party url in a popup, and when it is closed, the main program needs to fire some event (say display a message). Any suggestion is welcome.

  • Check this https://stackoverflow.com/questions/12158190/fire-a-function-on-window-close – Deepak Kumar T P Dec 26 '17 at 09:25
  • the window object itself is null, so calling beforeunload method also throws JavaScript runtime error: Unable to set property 'onbeforeunload' of undefined or null reference – Sourav Bhattacharya Dec 26 '17 at 09:34
  • You should set after opening the window ie in openWin() after myWindow = window.open('...'); – Deepak Kumar T P Dec 26 '17 at 09:37
  • myWindow object is null immediately after that line, when the url is of an external domain, in IE11. So neither onunload nor onbeforeunload works. If instead of myWindow.close, i try window.close, it shows a prompt and then closes the parent window instead of the popup. Can you please paste the code here if it worked for you. – Sourav Bhattacharya Dec 26 '17 at 09:55

0 Answers0