我想在网页上创建一个链接,将关闭浏览器中当前活动的选项卡,而不关闭浏览器中的其他选项卡。当用户单击关闭链接时,将出现一条警告消息,要求用户使用“YES”和“NO”两个按钮确认。如果用户单击“是”,关闭该页面,如果“否”,什么都不做。

怎样才能做到呢?有什么建议吗?


当前回答

这是可能的。我搜索了整个网络,但有一次当我做微软的一项调查时,我终于得到了答案。

试试这个:

window.top.close();

这将为您关闭当前选项卡。

其他回答

<button class="closeButton" style="cursor: pointer" onclick="window.close();">Close Window</button>

这对我来说很有效

试试这个

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

这是可能的。我搜索了整个网络,但有一次当我做微软的一项调查时,我终于得到了答案。

试试这个:

window.top.close();

这将为您关闭当前选项卡。

您可以尝试使用JavaScript + HTML来欺骗浏览器关闭当前窗口:

JS:

function closeWindow() {
 if(window.confirm('Are you sure?')) {
  window.alert('Closing window')
  window.open('', '_self')
  window.close()
 }
 else {
  alert('Cancelled!')
 }
}

HTML:

Some content
<button onclick='closeWindow()'>Close Current Window!</button>
More content

在FF 18和Chrome 24测试成功:

插入头部:

<script>
    function closeWindow() {
        window.open('','_parent','');
        window.close();
    }
</script> 

HTML:

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

感谢马科斯·j·德雷克。