Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery ajax call on click, only works once

Tags:

jquery

ajax

I have this simple jquery code. On click it gets the URL of the tag, loads that page next to the current content, slides it and removes the old content. The state of the page is EXACTLY the same as before, same elements no extra classes or styles. The problem is that the next ajax call just doesn't work. Maybe I need to .unbind() something?

I'm new to jquery and javascript so i'm quite lost. Thanks a lot for your help :)

<script  type="text/javascript">
    $(document).ready(function(){ 
        loadPage();
    });
    function loadPage(url) {
        if (url == undefined) {
            $('body').load('index.html header:first,#content,footer', hijackLinks);
        } else {
            $.get(url , function(data) {
                $('body').append(data);
                $('body>meta').remove();
                $('body>link').remove();
                $('body>title').remove();
                $('body').append(direction);
                sm = $(window).width();
                if(direction == "leftnav"){
                    $('body>header:last,body>#content:last,footer:last').css("left", "-" + sm + "px");
                    footerheight = $('body>#content:last').outerHeight(false) + $('body>header:last').outerHeight(true) ;
                    $('footer:last').css("top", footerheight);
                    $('body>header,body>#content,footer').css("-webkit-transition-duration","0.5s")
                    $('body>header,body>#content,footer').css("-webkit-transform","translate(" + sm + "px,0px)");
                };
                if(direction != "leftnav"){
                    $('body>header:last,body>#content:last,footer:last').css("left", sm + "px");
                    footerheight = $('body>#content:last').outerHeight(false) + $('body>header:last').outerHeight(true) ;
                    $('footer:last').css("top", footerheight);
                    $('body>header,body>#content,footer').css("-webkit-transition-duration","0.5s")
                    $('body>header,body>#content,footer').css("-webkit-transform","translate(-" + sm + "px,0px)");
                };
                setTimeout(function(){
                    $('body>header:not(:last),body>footer:not(:last),body>#content:not(:last)').remove()
                },500); 
                setTimeout(function() {
                    $('body>header,body>footer,body>#content').removeAttr('style') 
                },500);
            });
        }
    }
    function hijackLinks() {
        $('a').click(function(e){
            e.preventDefault();
            loadPage(e.target.href);
        direction = $(this).attr('class');
        });
    }
</script>
like image 324
cmplieger Avatar asked Nov 28 '22 03:11

cmplieger


2 Answers

Since you are loading content dynmically which is replacing the contents of your body your event handler is most likely is not going to remain.

To fix this you need to adjust your click handler to use either live() or delegate()

$('a').live("click", function(e){
    e.preventDefault();
    loadPage(e.target.href);
    direction = $(this).attr('class');

});
like image 65
Mark Coleman Avatar answered Nov 30 '22 16:11

Mark Coleman


for anyone reading post 2013 'on' is the new way of doing it now 'live' has been depreciated (example from wordpress pagination):

/*******ajax pagination*******/
jQuery(document).on('click', '#pagination a', function(e){  
  e.preventDefault();  
  var link = jQuery(this).attr('href');  
  jQuery('#content').html('Loading...');  //the 'main' div is inside the 'content' div
  jQuery('#content').load(link+' #main');
}); 

the .live() function has been depreciated so use .on() instead. obviously the jQuery library is needed too.

like image 27
timhc22 Avatar answered Nov 30 '22 16:11

timhc22