Question:
I'm trying to close a tab on my site, I've already tried using the following commands:
-
window.open('','_self',''); window.close();
-
window.close()
-
self.close()
-
var win = window.open("","_self"); win.close();
-
window.parent.close();
-
window.top.close()
-
top.open('','_self',''); top.close();
Many of these work fine in Chrome and IE, but in Mozilla it doesn't work, it only works if I run the command in the console in a new tab, but not in my site's tab.
Does anyone have any tips?
Answer:
Hello, to execute window.close() the action must come from an event (I always use the click), because I don't know if it works for other events.
So if you do:
document.getElementById('btn').addEventListener('click', function()
{
window.close();
}, false);
The tab will be closed, this is valid for tabs on your site, in case of popup there is no such problem as long as the popup was opened from your site! Popup example:
var popup = window.open("http://www.teste.com.br");
popup.close();