jQuery 如何使用javascript关闭当前选项卡?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18857363/
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
How to close current tab using javascript?
提问by Carlos
I want to close current tab using javascript/Jquery. But I did not find any solution. I read that the window.close() only works with window which is open with window.open()
method. So is there any way to send command to system that user press ctrl+w which is also close this window. fiddle
我想使用 javascript/Jquery 关闭当前选项卡。但我没有找到任何解决方案。我读到 window.close() 仅适用于使用window.open()
方法打开的窗口。那么有什么方法可以将命令发送到用户按 ctrl+w 的系统,这也会关闭此窗口。小提琴
function down(){
window.close()
}
回答by
It is possible. I searched the whole internet for this, but once when i took one of Microsoft's survey, I finally got the answer.
有可能的。我在整个互联网上搜索了这个,但有一次当我参加微软的一项调查时,我终于得到了答案。
try this:
尝试这个:
window.top.close();
this will close the current tab for you.
这将为您关闭当前选项卡。
回答by Kukulza
This seems to work from a previous thread: https://stackoverflow.com/a/18776480/3642027
这似乎从以前的线程工作:https: //stackoverflow.com/a/18776480/3642027
However, from my testing, it only works if it's a fresh link opened with target="_blank"
.
但是,根据我的测试,它仅在使用target="_blank"
.
回答by Bernabé Tavarez
I suggest this little function JS, it works on Chrome 54.0.2840.99
我建议使用这个小函数 JS,它适用于 Chrome 54.0.2840.99
function closeWin() {
window.top.close();
}
setTimeout(function(){
closeWin()
}, 1000);
回答by BenjeminStar
Here a HTML Code witoutjavascript:
这是一个没有javascript的 HTML 代码:
<a href="about:blank" target="">✖</a>
Here with a nice design:
这里有一个漂亮的设计:
<a style="color:black; text-decoration:none;" href="about:blank" target="">
<button style="position: fixed; top:2px; right:2px; border: 1px solid black; background: red; cursor: pointer; z-index: 2;">
✖
</button>
</a>
回答by Mbotet
Since the actual answer is obsolete because of security issues the closest working solution i found today is
由于安全问题,实际答案已过时,我今天找到的最接近的工作解决方案是
open(location, '_self').close();
打开(位置,'_self')。关闭();
I also recommend to check this thread where i got the code from its many authors: How to close current tab in a browser window?
我还建议检查我从许多作者那里获得代码的线程: 如何关闭浏览器窗口中的当前选项卡?
回答by sami ullah
I searched about it, someone wrote you can not close it until unless, you opened by code or pro-grammatically It was convincing but I found later and it worked for me. hope so it help you too.
我搜索了它,有人写道你不能关闭它,除非你通过代码或编程方式打开它是有说服力的,但我后来发现它对我有用。希望它也能帮助你。
var win = window.open("about:blank", "_self");
var win = window.open("about:blank", "_self");
win.close();
赢。关闭();