简体   繁体   中英

Javascript/Jquery : Back button not changing content at all

<script>
$(function(){
    $("a[rel='tab']").click(function(e){
        e.preventDefault(); 

        pageurl = $(this).attr('href');

        $.ajax({url:pageurl+'&rel=tab',success: function(data){
            $('#right_column').html(data);
        }});

        if(pageurl!=window.location){
            window.history.pushState({path:pageurl},'',pageurl);    
        }
        return false;  
    });
});

/* the below code is to override back button to get the ajax content without reload*/
$(window).bind('popstate', function() {
    $.ajax({url:location.pathname+'&rel=tab',success: function(data){
        $('#right_column').html(data);
    }});
});
</script>

I pulled this code off a demo and am modifying it to fit my particular project; however, am attempting to run it as it is to test out features. The demo worked perfectly. The only major difference is they are using jquery 1.4.4 and I am using jquery 1.9.1. I cannot seem to get the back button to work correctly. The url changes when hitting back; however, the #right_column doesn't update at all. I copied this code directly off a demo and adjusted the div id to match mine, and it still doesn't work. The below line of code is the questionable code.

/* the below code is to override back button to get the ajax content without reload*/
    $(window).bind('popstate', function() {
        $.ajax({url:location.pathname+'&rel=tab',success: function(data){
            $('#right_column').html(data);
        }});
    });

Also, can I use location.pathname.replace('index.php', 'view.php') ? Not sure if this is the correct way of writing that particular code to replace index.php?variables... with view.php?variables... to load that page into the right column. See my other post if this part of the question confuses you... javascript/jquery: Need to substring in jquery (modified code)

For those that this may help, this ended up fixing my code and it now responds to back button.

$(window).on('popstate', function() {
    $.ajax({url:$(location).attr('href').replace('index.php', 'rightcolumn.php') +'&rel=tab',success: function(data){
        $('#right_column').html(data);
    }});
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM