Javascript 关闭当前选项卡
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14373625/
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
Close Current Tab
提问by uvinod
I have a close link on my web page. I would like to function it to close the current tab when click it. I have written
我的网页上有一个关闭链接。我想在单击它时关闭当前选项卡。我已经写了
<a href="#" onclick = "javascript:self.close();">close</a>
The above code seems to be working well in Internet Explorer. But it is not working in Mozilla Firefox and Google Chrome. Please help me to resolve this.
上面的代码似乎在 Internet Explorer 中运行良好。但它不适用于 Mozilla Firefox 和 Google Chrome。请帮我解决这个问题。
回答by Brian Ustas
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 Adam M
As of Chrome 46, a simple onclick=window.close()does the trick. This only closes the tab, and not the entire browser, if multiple tabs are opened.
从 Chrome 46 开始,一个简单的onclick=window.close()就可以了。如果打开多个选项卡,这只会关闭选项卡,而不是整个浏览器。
回答by Nayana Priyankara
You can use below JavaScript.
您可以使用下面的 JavaScript。
window.open('','_self').close();
In a HTML you can use below code
在 HTML 中,您可以使用以下代码
<a href="javascript:close_window();">close</a>
I have tried this in Chrome 61 and IE11 it is working fine. But this is not working with Firefox 57. In Firefox we can only close, windows which opened using below command.
我已经在 Chrome 61 和 IE11 中尝试过它,它工作正常。但这不适用于 Firefox 57。在 Firefox 中,我们只能关闭使用以下命令打开的窗口。
window.open()
回答by S0AndS0
Found a one-liner that works in Chrome 66 from: http://www.yournewdesigner.com/css-experiments/javascript-window-close-firefox.html
从以下位置找到了一个适用于 Chrome 66 的单线:http: //www.yournewdesigner.com/css-experiments/javascript-window-close-firefox.html
TLDR: tricks the browser into believing JavaScirpt opened the current tab/window
TLDR:诱使浏览器相信 JavaScirpt 打开了当前选项卡/窗口
window.open('', '_parent', '').close();
So for completeness
所以为了完整性
<input type="button" name="close" value="close" onclick="window.close();">
Though let it also be noted that readers may want to place this into a function that fingerprints which browsers require such trickery, because Firefox 59 doesn't work with the above.
虽然还要注意的是,读者可能希望将其放入指纹浏览器需要这种技巧的功能中,因为 Firefox 59 不适用于上述功能。
回答by IamGuest
Try this:
尝试这个:
<script>
var myWindow = window.open("ANYURL", "MyWindowName", "width=700,height=700");
this.window.close();
</script>
This worked for me in some cases in Google Chrome 50. It does not seem to work when put inside a javascript function, though.
在某些情况下,这在 Google Chrome 50 中对我有用。但是,当放入 javascript 函数时它似乎不起作用。
回答by ananth vivekanand
Use this:
用这个:
window.open('', '_self');
This only works in chrome; it is a bug. It will be fixed in the future, so use this hacky solution with this in mind.
这仅适用于 chrome;这是一个错误。它将在未来修复,因此请牢记这一点,使用这个 hacky 解决方案。
回答by Anurag Ratna
Try this:
尝试这个:
window.open('', '_self').close();

