You could change your code to use "on" with a specific class:
$('#menu').on('click', 'a', (function (e) {
e.preventDefault();
var link = $(this);
if (!link.hasClass('loading') {
link.addClass('loading');
var href = link.attr('href');
$.ajax({
url: href,
dataType: 'html',
success: function (html) {
// AJAX STUFF HERE
},
complete: function() { link.removeClass('loading'); }
});
}
});
You only have one binding now (more efficient) and it will take the ajax action when the anchor does not have a "loading" class. Note that this will require jQuery 1.7+
EDIT (x2)
Per Dan's comments, the above code was updated for correctness. That said, you could make this slightly more efficient by changing the html and js to the following. Note that this means that users without JavaScript will simply follow the links.
<ul id='menu'>
<li><a href='/link1.php'>Link 1</a></li>
<li><a href='/link2.php'>Link 2</a></li>
<li><a href='/link3.php'>Link 3</a></li>
</ul>
$('#menu')
.on('click', 'a:not(.loading)', (function () {
var link = $(this).addClass('loading');
$.ajax({
url: link.attr('data-href'),
dataType: 'html',
success: function (html) {
// AJAX STUFF HERE
},
complete: function() { link.removeClass('loading'); }
})
})
// Remove anchor tag href to eliminate possibility of following the link versus ajax load
.find('a').each(function() {
$(this).attr('data-href', $(this).attr('href')).removeAttr('href');
});