Duplicate requests

2

(function(){
                $('a').on('click', function(e){
                    e.preventDefault();
                    var url = $(this).attr('href');
                    $.get(url, function(html){
                    $('.main').empty().append(html);
                    });
                });
            })();

I have this function to avoid the reload with postback, I only load a few elements inside the div "main". Surely because of not being the best way to fulfill this task, the request is duplicated every time I click on a link. increases progressively in addition, the first click doubles twice, the second 3, the third 9 etc ...

Any solution or other function that helps me do this? Thank you very much in advance.

They have already solved my problem, but if they think there is a better function, I would appreciate the help.

    
asked by Andres 30.09.2018 в 18:24
source

1 answer

1

Put before $('a').on('click', function(e){ a $('a').off("click") , your code would be like this:

(function(){

  $('a').off("click")
  $('a').on('click', function(e){
    e.preventDefault();
    var url = $(this).attr('href');
    $.get(url, function(html){
      $('.main').empty().append(html);
    });
  });

})();
    
answered by 30.09.2018 / 20:32
source