简体   繁体   English

单击目标_blank链接后关闭窗口

[英]Close the window after clicking target _blank link

单击带有href _blank的链接后2秒钟如何关闭当前窗口。

<a href="https://google.com" target="_blank"></a>

Maybe you can handle click event and from that open window and taking the reference of opened window and close it after timeout. 也许您可以处理click事件,并从该打开的窗口中获取已打开窗口的引用,并在超时后将其关闭。

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <button onclick="openWin()">Open Window</button>
</body>

  <script>
    var myWindow;

function openWin() {
    myWindow = window.open("http://www.google.com", "myWindow", "width=200,height=100");
    setTimeout(closeWin, 2000)
}

function closeWin() {
    myWindow.close();
}
  </script>
</html>

The window which has the link can not be closed by Javascript. 包含链接的窗口无法用Javascript关闭。

only the window which is opened by a script can be closed by that script. 只有脚本打开的窗口可以被该脚本关闭。

so because your window with the link is not opened by a script, so it can not be closed by a script. 因此,因为带有链接的窗口不是由脚本打开的,所以无法由脚本关闭。

but there are people suggesting some hacky ways. 但有人建议采取一些骇人听闻的方式。 check the following link: 检查以下链接:

How can I close a browser window without receiving the "Do you want to close this window" prompt? 如何关闭浏览器窗口而又没有收到“是否要关闭此窗口”提示?

the problem with hacks is that its not guaranteed to work always on all browser. hacks的问题在于,它不能保证始终在所有浏览器上都能正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM