Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to delay default action?

Tags:

jquery

ajax

in jquery, i need to delay the default behavior of anchors, so that in the meantime, ajax post request can complete itself.

preventDefault or returning false in .click event wont do, as I still need to navigate to those pages. window.location = href obviously fails because some anchors do not have href attribute instead uses javascript. I do not control these pages.


1 Answers

You just use the 'success' callback within the ajax function for jQuery itself

 $.ajax({
   type: "POST",
   url: "some.php",
   data: "name=John&location=Boston",
   success: function(msg){
     // do what you need to here

     return true;
   }
 });

Then it will wait until the Ajax has completed before moving on to the success callback

like image 134
jakeisonline Avatar answered Mar 01 '26 05:03

jakeisonline



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!