是否可以延迟<a target="_blank"> click?

时间:2018-08-11 17:04:19

标签: javascript

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?

2 个答案:

答案 0 :(得分:2)

阅读答案:Open a URL in a new tab (and not a new window) using JavaScript

但是我想您可以模拟点击<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 :(得分:1)

您可以在将第二个参数指定为_blank的同时使用window.open()函数:

window.open(URL, "_blank")target=_blank具有相同的作用。