javascript 从javascript关闭firefox选项卡

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19302387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 15:05:11  来源:igfitidea点击:

Close firefox tab from javascript

javascriptfirefox

提问by user2867978

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.

我想从 JavaScript 关闭 Firefox 选项卡。请不要误解我的意思。我不是要关闭弹出窗口,而是要关闭选项卡。我知道 JavaScript 无法关闭它尚未打开的窗口。因此我尝试了下面的代码,但它适用于所有浏览器,但不适用于 Firefox。

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

回答by Nepomuk Fr?drich

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

如果您有一个单用户/少用户页面并且可以访问 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 测试)

回答by Web User

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. Because the rule appears to be that JavaScript can close a window that was opened by JavaScript.

这将关闭窗口/选项卡。它可以被描述为黑客攻击。本质上,它使浏览器误以为当前窗口是由 JavaScript 打开的窗口/选项卡。因为规则似乎是 JavaScript 可以关闭由 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.

它适用于 Chrome、Firefox。Internet Explorer 需要一些额外的处理来解释自 IE 6 到 IE 8+ 以来的不同行为。如果有人感兴趣,我也包括在内。

            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();
            }

回答by jie

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

你可以试试这个代码。如果是火狐浏览器。

gBrowser.removeCurrentTab();

回答by subeesh

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

根据 Mozilla Firefox Deverlopers 论坛,现在不可能。参见下文。

"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:脚本不能关闭窗口,它们没有打开)”