简体   繁体   English

从javascript关闭firefox选项卡

[英]Close firefox tab from javascript

I want to close Firefox tab from JavaScript.我想从 JavaScript 关闭 Firefox 选项卡。 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.我知道 JavaScript 无法关闭它尚未打开的窗口。 Therefore I tried below code but it works in all browsers but not in Firefox.因此我尝试了下面的代码,但它适用于所有浏览器,但不适用于 Firefox。

window.open('','_self','');
Window.close();

If you have a single/few-user page and you have access to the Firefoxes you can change the about:config settings.如果您有一个单/少用户页面并且您可以访问 Firefox,您可以更改about:config设置。

dom.allow_scripts_to_close_windows = true

This might be a big security issue!这可能是一个很大的安全问题!

(Tested with Firefox 27 on Linux) (在 Linux 上用 Firefox 27 测试)

Here is something I learnt from a StackOverflow thread (unfortunately could not find it to link to this answer):这是我从 StackOverflow 线程中学到的东西(不幸的是找不到它来链接到这个答案):

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.本质上,它使浏览器误以为当前窗口是由 JavaScript 打开的窗口/选项卡。 Because the rule appears to be that JavaScript can close a window that was opened by JavaScript.因为规则似乎是 JavaScript 可以关闭由 JavaScript 打开的窗口。

It works in Chrome, Firefox.它适用于 Chrome、Firefox。 Internet Explorer needs a little extra treatment to account for varying behavior since IE 6 to IE 8+. Internet Explorer 需要一些额外的处理来解释自 IE 6 到 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();
            }

You can try this code.你可以试试这个代码。 If it is Firefox browser.如果是火狐浏览器。

 gBrowser.removeCurrentTab();

According to Mozilla Firefox Deverlopers forum, it is not possible now.根据 Mozilla Firefox Deverlopers 论坛,现在不可能。 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)" “过去,当您直接调用 window 对象的 close() 方法时,而不是在 window 实例上调用 close() 时,浏览器会关闭最前面的窗口,无论您的脚本是否创建了该窗口。现在不再如此; 出于安全原因,不再允许脚本关闭它们没有打开的窗口。(Firefox 46.0.1:脚本不能关闭窗口,它们没有打开)”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM