简体   繁体   English

如何关闭浏览器标签

[英]How to close browser tab

I am not able to close the current browser tab using javascript. 我无法使用javascript关闭当前的浏览器标签。

I tried- 我试过了-

    window.close(); //1

    window.open("", '_self');//2
    setTimeout(function(){
        window.close();
    },100);

    window.open('','_parent',''); //3
    window.close();

    window.top.opener=null; //4
    window.close();

Nothing is working for me in chrome chrome无法为我工作

仅允许使用window.open方法由脚本打开的窗口调用window.close方法。

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

在此线程上查看我的原始答案

As much as I know, you can't close browser tab using Javascript. 据我所知,您无法使用Javascript关闭浏览器标签。 This is not a common way to do... 这不是常见的做法...

I'm not sure if you can do this, because some browsers do not have any tabs at all... 我不确定您是否可以执行此操作,因为某些浏览器根本没有任何标签...

But try this, may be it work for you : 但是尝试一下,也许对您有用:

window.top.close();

reference : Another Question in stackoverflow 参考: stackoverflow中的另一个问题

OR : 要么 :

<a href="javascript:window.open('','_self').close();">close</a>

You can only close windows/tabs that you create yourself. 您只能关闭自己创建的窗口/选项卡。 That is, you cannot programmatically close a window/tab that the user creates. 也就是说,您不能以编程方式关闭用户创建的窗口/选项卡。

For example, if you create a window with window.open() you can close it with window.close() . 例如,如果使用window.open()创建一个窗口,则可以使用window.close()其关闭。

Please try to use the following 请尝试使用以下内容

window.top.close();

Hope, this would help! 希望这会有所帮助!

As already stated, you can only close windows/tabs that you previously opened yourself. 如前所述,您只能关闭先前打开的窗口/选项卡。 None of your code snippets comply with this. 您的代码段均不符合此要求。

If you want to close a child window from its parent you need to store a reference to it, eg: 如果要从父窗口关闭子窗口,则需要存储对其的引用,例如:

var child = window.open("");
setTimeout(function(){
    child.close();
},100);

Demo 演示版

If you want to close the child window from itself you need to run the code inside the window. 如果要从自身关闭子窗口,则需要在窗口内运行代码。

If you overwrite current document with a blank one your your code disappears. 如果用空白覆盖当前文档,则您的代码将消失。


Now, Chrome is a special beast. 现在,Chrome是一种特殊的野兽。 It opens each tab in a different process. 它以不同的过程打开每个选项卡。 That means that several good old techniques that involve different tabs do not work at all. 这意味着涉及不同选项卡的几种良好的旧技术根本不起作用。 For instance, you can't even use the target parameter in window.open() to share a window. 例如,您甚至不能使用window.open()中的target参数共享一个窗口。

<script src="~/Scripts/jquery-1.8.2.js"></script>
<script>
    var win;
    $(document).ready(function () {
        var url = 'this tab url';
        win = window.open(url, '_self');
        window.stop();                
    });
    function closeWindow() {
        win.close();
    }
</script>

Then in html 然后在HTML

<a href="javascript:closeWindow();">close</a>

If there are multiple tusk to load then wait some times by this 如果要加载多个象牙,请等待一段时间

setTimeout(
  function() 
  {
    //do something special
   //window.stop(); 
  }, 5000);

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

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