0

I have :

$('.clic').click(function () {
      event.preventDefault();
      var cliclink = $(this).attr('href');
      var clicanchor = $(this).text();
      $.post("addclic.php", { li: cliclink, an: clicanchor });
})

and

<a href="new_page.html" class="clic">my_link</a>

When i click on the link, the $.post is done but i'am no redirected to new_page.html. If i suppress event.preventDefault(), i'am correctly redirected to new_page.html but the $.post doesn't work.

I'd like the link and the $.post work simultaneously, but how ?....

Dan
  • 9,391
  • 5
  • 41
  • 73
  • 1
    Just do a redirect in the `$.post` success callback: http://stackoverflow.com/questions/503093/how-can-i-make-a-redirect-page-in-jquery-javascript – Joe Jun 13 '14 at 16:55
  • 1
    If you follow the link, it reloads the page and all scripts stop. You can't do it simultaneously. – Barmar Jun 13 '14 at 16:55
  • Take a look at this: http://stackoverflow.com/questions/2694876/jquery-how-to-call-a-custom-function-before-following-link – Samsquanch Jun 13 '14 at 16:57

1 Answers1

0

you can redirect using location.href and redirect to url in success call back function:

$('.clic').click(function () {

event.preventDefault();
var cliclink = $(this).attr('href');
var clicanchor = $(this).text();
$.post("addclic.php", 
{ 
  li: cliclink,
  an: clicanchor },
  function(response){

location.href=clicklink;

});
})
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160