javascript - Trigger function and open URL at the same time -


i creating counter counts number of clicks made anchor tag. need url opened @ same time. tried below code. page redirected before function executes.

is there better way this?

code:

$(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>


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -