Close Mozilla, IE, Chrome tab

Asked

Viewed 24,368 times

7

I am trying to close a tab of my site, I have tried to use 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 right in Chrome and IE, only Mozilla doesn’t work, it only works if you run the console command in a new tab, but not in my website tab.

Does anyone have any tips?

  • 3

    Since version 27 of Firefox, security with tab closing has increased, so even the hacks used in <27 versions have stopped working.

  • too bad, I really needed to close that tab. Thank you for the answer

4 answers

3

Hello, to run window.close() the action should come from an event ( I always use click ), because I do not 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 of your site, in case of popup there is no such problem, since the popup was opened from your site! Example in popup:

var popup = window.open("http://www.teste.com.br");
popup.close();

2

I found the same problem and solved using:

window.frames.closewindow();

This is because I work with a frame-based system, I did not test with another type of system. It’s worth a try.

2

Try to do it this way:

function tabClose() {
  var tab = window.open("","_self");
  tab.close();
}

I don’t use IE, but in Chrome and Firefox, it solves your problem, maybe in IE too, but I couldn’t test.

2

That’s how it worked on Chrome and IE. The options "_Blank" and "_self" do not solve, use "_top". Only the open tab will be closed, if it is the last tab the browser closes!

function tabClose() {
  var tab = window.open(window.location,"_top");
  tab.close();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.