3

Say I want to delay some seconds after clicking on a <a> link.

I have read some questions and found one here:

function delay (URL) {
    setTimeout( function() { window.location = URL }, 500 );
}

However, this would make the page opened in the current window. Not a new one.

How can I have the effect to make it work as target=_blank while NOT using window.open popups? Is that even possible?

AGamePlayer
  • 7,404
  • 19
  • 62
  • 119

2 Answers2

2

Read the answer: Open a URL in a new tab (and not a new window) using JavaScript

But I imagine you can emulate a click on <a>

<a id="link" style="display: none;" href="https://google.com" target="_blank">Google</a>
<button id="trigger">Trigger</button>

<script>
    const trigger = document.getElementById('trigger');
    trigger.addEventListener('click', function(event) {
      setTimeout(function() {
        document.getElementById('link').click();
      }, 500);
    })
</script>
1

You can use window.open() function while specifying the second argument as _blank:

window.open(URL, "_blank") have the same effect as target=_blank.

u-ways
  • 6,136
  • 5
  • 31
  • 47