Using win.close()
will close the window if win
is a variable that has the value of a window.open()
call stored in it.
So,
var win = window.open("//stackoverflow.com");
win.close();
will close the window opened by window.open()
.
However, window.close()
only works on a window that was opened by JavaScript. That is, you can't close a window that a user navigated to by clicking a link.
To control a window, you'd have this:
<a href="javascript:var win = window.open('//link.to/new_page');">Open Window</a>
Then, in your new window, you'd have this to close it:
<button onclick="win.close();">Close</button>