2013-10-10 114 views
5

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

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

不可能的,除非该窗口具有父。 Firefox更新了这种方式返回到其中一个版本,他们停止窗口关闭没有父窗口 –

+0

https://support.mozilla.org/en-US/questions/966137您只能在Firefox中关闭弹出窗口,而不是浏览器或浏览器选项卡 – Rohit

回答

3

下面是我从StackOverflow的线程学会(可惜找不到它链接到这个答案):

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

这将关闭窗口/标签。它可以被描述为黑客。本质上,它愚弄浏览器认为当前窗口是JavaScript打开的窗口/选项卡。因为规则似乎是JavaScript可以关闭由JavaScript打开的窗口。

它适用于Chrome,Firefox。从IE 6到IE 8+以后,Internet Explorer需要一些额外的处理来说明不同的行为。如果任何人有兴趣,我也包括这一点。

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

感谢您的快速响应,但上面的代码似乎无法在FF 24中工作。对于其他浏览器,我确实有代码,但它只有FF 24的问题。最新的FF浏览器的问题是,它们可以确定,如果从JS打开的窗口是自动浏览器中的弹出窗口或窗口。所以gievn代码将不起作用。 (document.URL,'_ self','resizable = no,top = -245,width = 250,height = 250,scrollbars = no'); window.close(); – user2867978

+0

看起来他们在FF中进一步收紧了[JS安全模型](https://support.mozilla.org/en-US/questions/966137)。 –

+0

我想你是指这个问题: http://stackoverflow.com/questions/2076299/how-to-close-current-tab-in-a-browser-window 也许这个问题应该被标记为重复。 – user2345998

5

如果你有一个/少数用户的页面,你可以访问Firefoxes您可以更改about:config设置。

dom.allow_scripts_to_close_windows = true 

这可能是一个很大的安全问题!

(在Linux上使用Firefox 27进行测试)

2

您可以试试这段代码。 如果是Firefox浏览器。

gBrowser.removeCurrentTab();