0

I'm trying to do something simple, capture any click event and send the url to a php script.

With the alert(a); the ajax.php will be call every single time, if I remove it, once every 20 clicks will work, I wonder if it's because the alert(a) slow things down ?

$('a').click(function(){
    var a = $(this).attr('href');
    $.ajax({
        type: "POST",
        url: "/ajax.php",
        data: { b1: a , b2: "456" },

    });
    alert(a);
});
4
  • 3
    what is your question here Commented Apr 20, 2013 at 10:16
  • jsfiddle.net/ukd2X Commented Apr 20, 2013 at 10:20
  • As far as i can see, the /ajax.php will get called for every click, irrespective of the alert Commented Apr 20, 2013 at 10:35
  • My issue / question is that it does not Commented Apr 21, 2013 at 7:10

1 Answer 1

1

If you click on the A, there is still the event to go to another page. So do this:

$('a').click(function(e){
    e.preventDefault();
    var a = $(this).attr('href');
    $.ajax({
        type: "POST",
        url: "/ajax.php",
        data: { b1: a , b2: "456" },
        success : function(){
            document.location = a;
        }
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

If I add e.preventDefault(); then it will load ajax.php but not follow the link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.