Javascript 如何关闭浏览器选项卡

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32861649/
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-08-23 14:17:34  来源:igfitidea点击:

How to close browser tab

javascriptjquery

提问by Shubham

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 中没有什么对我有用

回答by QBernard

window.close method is only allowed to be called for windows that were opened by a script using the window.open method.

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

回答by Mada Aryakusumah

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

See my original answer on this thread

请参阅我在此线程上的原始答案

回答by Kiyarash

As much as I know, you can't close browser tab using Javascript. This is not a common way to do...

据我所知,您无法使用 Javascript 关闭浏览器选项卡。这不是一种常见的做法...

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>

回答by ozil

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

回答by álvaro González

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, e.g.:

如果要从父窗口关闭子窗口,则需要存储对它的引用,例如:

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. 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.

现在,Chrome 是一种特殊的野兽。它在不同的过程中打开每个选项卡。这意味着涉及不同选项卡的几种很好的旧技术根本不起作用。例如,您甚至不能使用 window.open() 中的 target 参数来共享窗口。

回答by asalam345

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

回答by Abhijeet K.

Please try to use the following

请尝试使用以下

window.top.close();

Hope, this would help!

希望,这会有所帮助!