I am using jQuery load() function to load some pages into container. Here is the code:
$('div.next a').live('click',function() {
$('.content').load('page/3/ #info','',function(){
//do something
});
return false;
});
Everything works just fine but the problem is when I quickly double click the div.next link, from console I see that it loads the page twice because I did a quick double click. I could even make it 3 clicks and it will load it 3 times and show in console smth like that:
GET http://site/page/3/ 200 OK 270ms
GET http://site/page/3/ 200 OK 260ms
My question is how to prevent such double clicking and not to let load the target page more then once no matter how many times it was clicked.
Thank you.
Whatever happened to good ol’ JavaScript? Why are you all trying to figure it out with pure jQuery?
As a side note, never never never use
.live(). Use .delegate instead like:Why? Paul Irish explains: http://paulirish.com/2010/on-jquery-live/
To answer your comment…
This could happen if you have your delegate function nested inside your AJAX call (
.load(),.get(), etc). Thediv.nexthas to be on the page for this to work. Ifdiv.nextisn’t on the page, and this isn’t nested, just do this:http://api.jquery.com/delegate/
Delegate needs the selector to be the parent of the dynamically added element. Then, the first parameter of delegate (
div.next ain the last example) is the element to look for within the selected element (#wrapper). The wrapper could also bebodyif it’s not wrapped in any element.