0

How i can close the current window 2 sec after a link with an href _blank is clicked .

<a href="https://google.com" target="_blank"></a>
Inus Saha
  • 1,918
  • 11
  • 17
Yusuf HR
  • 92
  • 2
  • 10

2 Answers2

2

Maybe you can handle click event and from that open window and taking the reference of opened window and close it after timeout.

<!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>
Dhaval
  • 389
  • 1
  • 10
  • thanks i know that but i need a new way to open and close a new tab without browser ask you to allow popups – Yusuf HR Jun 23 '18 at 10:41
0

The window which has the link can not be closed by 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.

Inus Saha
  • 1,918
  • 11
  • 17