Asynchronous logging in JQuery

Javascript code:$.ajaxSetup( async: true); $(“a”).live(‘click’, function (event) $.get(logsomething-URL););ASP.NET code on the logsomething page on the server:Thread.Sleep(10000);When I click a link I have to wait for a response from the server. In this case I have to wait 10 seconds before I am forwarded to the …

via Asp.Net Developed Tutorials » Search Results » ajax:

Asynchronous logging in JQuery

Javascript code:
$.ajaxSetup(
async: true
);

$(“a”).live(‘click’, function (event)
$.get(logsomething-URL);
);ASP.NET code on the logsomething page on the server:
Thread.Sleep(10000);When I click a link I have to wait for a response from the server. In this case I have to wait 10 seconds before I am forwarded to the page I requested.
Is it possible to prevent this?
…………………………………….

First you don’t need:
$.ajaxSetup(
async: true
);because AJAX requests are asynchronous by default (that’s what the A stands for: asynchronous).
Next you could return false from the click handler to prevent the default action (which is to follow the href) and in the AJAX success handler perform the actual redirect:
$(‘a’).live(‘click’, function (event)
// store the location to redirect to later in the success callback
var href = this.href;
$.get(‘logsomething-URL’, function(result)
// Now after the AJAX call successfully returned
// perform the redirect
window.location = href;
);
return false;
});
…………………………………….

Something like this ought to do it – basically you don’t want the link to act like a link:
$(“a”).live(‘click’, function (event)
var linkTarget = $(this).attr(‘href’);
event.preventDefault();
$.get(logsomething-URL, function()
window.location = linkTarget;
);
});
…………………………………….

I would not use $.get but instead use $.ajax – it lets you specify more. If this doesn’t work, try have a look at
http://api.jquery.com/jQuery.ajax/
$.ajax(
url: ‘logsomething-URL’,
success: function(data)
alert(‘Success!’);
,
global: false
});

For more info: Asynchronous logging in JQuery

Asp.Net Developed Tutorials » Search Results » ajax

Asynchronous logging in JQuery

Share this post:

Related Posts

Leave a Comment