4

I want to close Firefox tab from JavaScript. Please do not misunderstand me. I am not trying to close popup window but tab. I know JavaScript cannot close the window which it has not opened. Therefore I tried below code but it works in all browsers but not in Firefox.

window.open('','_self','');
Window.close();
mkj
  • 2,761
  • 5
  • 24
  • 28
user2867978
  • 49
  • 1
  • 1
  • 3
  • Not possible unless the window has a parent. Firefox updated this way back in one of the versions where they stopped the window close without the parent window – Akshay Khandelwal Oct 10 '13 at 17:31
  • https://support.mozilla.org/en-US/questions/966137 You can only close popup windows in firefox, not the browser or the browser tab – Rohit Oct 10 '13 at 17:33

4 Answers4

6

If you have a single/few-user page and you have access to the Firefoxes you can change the about:config settings.

dom.allow_scripts_to_close_windows = true

This might be a big security issue!

(Tested with Firefox 27 on Linux)

Nepomuk Pajonk
  • 2,972
  • 1
  • 19
  • 29
3

Here is something I learnt from a StackOverflow thread (unfortunately could not find it to link to this answer):

window.open(document.URL,'_self','resizable=no,top=-245,width=250,height=250,scrollbars=no');
window.close();

This closes the window/tab. It can be characterized as a hack. Essentially, it fools the browser into thinking that the current window is a window/tab opened by JavaScript. Because the rule appears to be that JavaScript can close a window that was opened by JavaScript.

It works in Chrome, Firefox. Internet Explorer needs a little extra treatment to account for varying behavior since IE 6 to IE 8+. I am including that too, if anyone's interested.

            var Browser = navigator.appName;
            var indexB = Browser.indexOf('Explorer');
            if (indexB > 0) {

                var indexV = navigator.userAgent.indexOf('MSIE') + 5;
                var Version = navigator.userAgent.substring(indexV, indexV + 1);

                if (Version >= 7) {
                    window.open('', '_self', '');
                    window.close();
                }
                else if (Version == 6) {
                    window.opener = null;
                    window.close();
                }
                else {
                    window.opener = '';
                    window.close();
                }
            }
            else {
                window.close();
            }
Web User
  • 7,438
  • 14
  • 64
  • 92
  • 3
    Thank you for your quick response but the above code does not seem to work in FF 24. For other browsers i do have the code but its only the issue with FF 24. The problem with latest FF browser is that they can identify that if the window opening from JS is either a popup window or a window in self browser. So the gievn code will not work. window.open(document.URL,'_self','resizable=no,top=-245,width=250,height=250,scrollbars=no'); window.close(); – user2867978 Oct 10 '13 at 19:01
  • Looks like they further tightened the [JS security model](https://support.mozilla.org/en-US/questions/966137) in FF. – Web User Oct 10 '13 at 19:54
  • I guess you mean this question: http://stackoverflow.com/questions/2076299/how-to-close-current-tab-in-a-browser-window Maybe this question should be marked as a duplicate. – user2345998 Jan 13 '15 at 14:27
3

You can try this code. If it is Firefox browser.

gBrowser.removeCurrentTab();
Pavel
  • 7,436
  • 2
  • 29
  • 42
jie
  • 31
  • 1
2

According to Mozilla Firefox Deverlopers forum, it is not possible now. Read below.

"In the past, when you called the window object's close() method directly, rather than calling close() on a window instance, the browser closed the frontmost window, whether your script created that window or not. This is no longer the case; for security reasons, scripts are no longer allowed to close windows they didn't open. (Firefox 46.0.1: scripts can not close windows, they had not opened)"

subeesh
  • 21
  • 3