Reputation: 109
I want a user to open a new tab (tab 2) with a button in tab 1.
In tab 2 I want the user to be able to close tab 2 by clicking a (nicely-styled) button.
The Mozilla documentation gives an example like this
var windowObjectReference;
function openRequestedPopup(url) {
windowObjectReference = window.open(url);
}
but if I use this on tab 2
function closeWindow() {
windowObjectReference = window.close();
}
the console tells me that windowObjectReference is not defined, so not passed to the next tab.
<button onclick=self.close() >Close</button>
also doesn't work. The console tells me that "self.close is not a function".
edit: the windowObjectReference is not passed to tab2, so using the close() method also does not work
function closeOpenedWindow() {
windowObjectReference.close();
}
The console tells me again that "windowObjectReference is not defined"
Upvotes: 0
Views: 62
Reputation: 28499
This is enough to close the current tab (that the button is placed on):
<button onclick="window.close()">Close</button>
Note that depending on the browser, this will only work when the current tab was opened by a script itself and not manually by the user.
Upvotes: 1