触发功能同时开启URL

Trigger function and open URL at the same time

我正在创建一个计数器,用于计算锚标记的点击次数。我还需要同时打开URL。我试过下面的代码。页面在函数执行前被重定向。

有更好的方法吗?

代码:

$(document).ready(function () {
 var $res = $('#counter').text($.cookie('link-count') || 0)
    
    $('a').click(function () {
        var count = parseInt($.cookie('link-count'), 10) || 0;

        count++;
        if (count > 10) {
            $(this).attr("href", "https://www.yahoo.com");
        }
        $.cookie('link-count', count);
        $res.text(count);
        location.href = $(this).attr("href");
        
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="www.google.com">Google</a>
<div id="counter">0</div>

var linkCount;
$(document).ready(function () {
 var $res = $('#counter').text(linkCount || 0)
    
   $('a').click(function (e) {
      
        e.preventDefault();
      
        var count = parseInt(linkCount) || 0;

        count++;
        if (count > 10) {
            $(this).attr("href", "https://www.yahoo.com");
        }
        linkCount = count;
        $res.text(count);
        location.href = $(this).attr("href")
      
        
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="www.google.com">Google</a>
<div id="counter">0</div>