tags:

views:

83

answers:

2

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.

A: 

Hi,

If some anchors don't have href, I guess you'll not be able to change their behaviour. For the rest, just use a timeout on the click event and get href in order to redirect after the timeout ends, using its callback funcion.

yoda
so this is impossible ?
if the "other" anchors use javascript to redirect, and you don't have control on it, yes.
yoda
+1  A: 

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

jakeisonline
yes but im talking about for links that do not have href url location. it is not possible to redirect within the success callback.
you just check within the success callback whether there is a href defined or not, if there is **not** then you return false. If there is, return true.
jakeisonline