首页 > 解决方案 > 重定向 onClick + target="_blank"

问题描述

我试图让以下情况发生:

这是我现在拥有的代码:

<script>
    function pageRedirect() {
      window.location.href = "https://freebrandingsurveys.com/instructions/";
    }      
</script>
<a href="https://twitter.com/intent/tweet?url=https://freebrandingsurveys.com" target="_blank" onclick="pageRedirect()">
<img src="twitter.png" alt="Twitter Share"></a>

问题是带有推文的新窗口永远不会打开,而用户只是直接进入网站上的新页面(/instructions)。

以为我可以在重定向上创建某种时间延迟(就像提到的这里),但它不起作用。关于如何实现这一点的任何见解?

标签: javascript

解决方案


当用户单击图像时,您希望发生两件事。尝试这个:

<script>
    function imgOnClick() {
      window.location.href = "https://freebrandingsurveys.com/instructions/"; // navigate to new page in current window
      window.open('https://twitter.com/intent/tweet?url=https://freebrandingsurveys.com', '_blank'); // open the twitter page on a new window 
    }      
</script>
<img src="twitter.png" alt="Twitter Share" onclick="imgOnClick()">

推荐阅读