如何在浏览器窗口中关闭当前标签页?

597
我希望在网页上创建一个链接,点击该链接可以关闭浏览器中当前激活的标签页而不关闭其他标签页。当用户单击关闭链接时,应出现一个警告消息,提示用户确认是否关闭,提供两个按钮,“是”和“否”。如果用户单击“是”,则关闭该页面;如果单击“否”,则不执行任何操作。
如何实现?有什么建议吗?

2
我可能来晚了,但浏览器有其防止的原因。<br><br> 想象一下你一遍又一遍地试图关闭一个窗口,但它不会关闭,因为它正在执行你问题中的“如果没有”的部分,即“什么也不做”。<br><br> 这肯定会让人恼火! - Muhammad Osama
不可能的。请阅读此内容:https://dev59.com/2WIj5IYBdhLWcg3w6JHU#19768082 - iamandrewluca
对于任何试图以编程方式关闭选项卡的人,我有两件事要补充(TL:DR;) 1 您只能关闭使用JavaScript打开的选项卡(如Ryan Joy所提到的)。 2,这里没有提到:只有当window.top.opener(“父”窗口)不为null时,您才能关闭选项卡。 - Nico
windows.close()在Angular中无法正常工作。它会给我一个警告:脚本只能关闭由它们打开的窗口。 - Margi212
您想关闭的页面,请粘贴以下代码:<a href="JavaScript.windows.close()"> 关闭</a>。 - Omid Shagiwal
23个回答

0
这是解决问题的一种方法,声明一个像这样的JavaScript函数。
<script>
  function Exit() {
     var x=confirm('Are You sure want to exit:');
     if(x) window.close();
   }
</script>

将以下行添加到HTML中,以使用<button>调用该函数。
<button name='closeIt' onClick="Exit()" >Click to exit </Button>

0
As stated previously by RixTheTyrunt here, due to security, Modern browser behavior does not allow window.close to work unless is opened by window.open or under other strict circumstances(like opening a new tab with minimal interaction).
When a page attempts to post a form, once that form interacts; window.close does not work(at least for me in several browsers: ff, edge, chrome...).

I liked a lot the solution by Thailandian, but contains document.write which is  frown upon.

I tried several solutions from this page and from other, but is by design.
This is what I considered a workaround. The main idea is to let the User know.

Best regards,
Thanks for reading.  

<script>
  // below will attempt to close if possible 
  function closing() {
    window.close(self);
    window.close('', '_parent', '');
    window.close('', '_self', '');
    // after several attempts will let the User know
    // below, will display a Full Screen Div with a Message for the User
    document.getElementById("msgDiv").style.display = 'block';
    // below, will Hide your Current Page Div to allow only the Previous Message
    document.getElementById("currentPage").style.display = 'none';
    // Below a timer to reload 
    setTimeout(function() {
      document.location.reload(true);
    }, 3000);
  }
</script>


<!-- Below is the Message for the User -->
<div align="center" id="msgDiv" style="display:none;width:100%;height:100vh">
  <h1><br>Unable to close Tab/Window with Scripts.<br><br>Please Close Tab/Window Manually in the Browser.<br><br>Thanks.<br><br><button style="cursor:pointer" onclick='document.location.reload(true);'>Reload</button></h1>
</div>
<!-- Below is the Current Page -->
<div id="currentPage" style="display:block;">
  <div style="position: absolute; right:1%; top: 2px; height: 22px; width: 30px; padding: 0em;" title="EXIT">
    <a href="#" onclick="closing()" style="cursor:pointer;text-decoration: underline;font-weight:900; background: tomato; border: 4px outset red; border-radius: 4px;">&times;</a></div>

  Your Page Content Should be Here <br>
  <br>
  <br>
  <form method="post" action="#">
    <div align="center" style="padding:14%;">
      <input style="font-size:1.5em" type="text" name="text" placeholder="Favorite Word" autofocus="autofocus">
      <input style="font-size:1.5em;cursor:pointer" type="submit" value="Go"></div>
  </form>
  <pre style="white-space:pre-wrap;">
As stated previously by RixTheTyrunt here, due to security, Modern browser behavior does not allow window.close to work unless is opened by window.open or under other strict circumstances(like opening a new tab with minimal interaction).
When a page attempts to post a form, once that form interacts; window.close does not work(at least for me in several browsers: ff, edge, chrome...).

I liked a lot the solution by Thailandian, but contains document.write which is  frown upon.

I tried several solutions from this page and from other, but is by design.
This is what I considered a workaround. The main idea is to let the User know.

Best regards,
Thanks for reading.  
</pre>
</div>


很遗憾,对我来说这个方法不起作用,反而重新加载了页面 :c - undefined
刷新是在关闭尝试之后的一部分行为。 用户至少应该看到“无法使用脚本关闭标签/窗口...”现代浏览器的行为不允许使用window.close,除非是由window.open打开的或在其他严格的情况下(比如通过最小交互打开一个新标签页)。 当页面尝试提交表单时,一旦该表单进行交互,window.close将无法工作(至少对我来说,在几个浏览器中如此:ff,edge,chrome...)。这被认为是一种没有使用document.write的变通方法,这是不被赞同的。这个想法是让用户知道。最好的问候, - undefined

-4
这是如何创建这样一个链接的方法: <a href="javascript:if(confirm('关闭窗口?'))window.close()">关闭</a>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接