skip to Main Content

I would like to popup a winodw with a button and close by another one, here is my code:

The window could be successfully open but cannot close, no any reponses by clicking the button. Is it a browser problem or what?

var newWin;

function openWindow() {
  newWin = window.open('https://www.google.com', '', 'width=400,height=1000');
}

function closeWindow() {
  newWin.close();
}
<button onclick="openWindow()">Open Window</button>
<button onclick="closeWindow()">Close Window</button>

2

Answers


  1. It could be reference-loss. If you call openWindow multiple times, the ref to the previous window will be lost and you won’t be able to close it.

    Or the browser does not allow to close it.

    Login or Signup to reply.
  2.  window.close 
    

    is true even after you open a window using script.

    Reason for this when you click on close window button it triggers mouse click outside window and the window gets minimized. So the window is null at that moment so your closeWindow function won’t get any window to close*

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search