history.pushstate fails browser back and forward button

后端 未结 1 750
时光取名叫无心
时光取名叫无心 2020-12-10 12:09

I\'m using jQuery to dynamically load content in a div container.

The server side code detects if the request is AJAX or GET.

I want the browsers back/forwa

相关标签:
1条回答
  • 2020-12-10 12:54

    @Barry_127, see if this will work for you: http://jsfiddle.net/SX4Qh/

    $(document).ready(function(){
        window.onpopstate =  function(event) {
            alert('popstate fired');
            $('#ajaxContent').fadeOut(function() {
                $('.pageLoad').show();
                $('#ajaxContent').html('')
                                 .load($(this).attr('href'), function() {
                                    $('.pageLoad').hide();
                                    $('#ajaxContent').fadeIn();
                                 });
            });
        };
    
        $('.ajax').on('click', function(event) {
            event.preventDefault();
            alert('pushstate fired');
            window.history.pushState({state:'new'},'', $(this).attr('href'));
        });
    
     });
    

    If you take a look at the fiddle I provided and click the button, the alert will fire showing that you are pushing a new state. If you then proceed to click the back button once the pushstate has fired, you will see that the previous page (or popstate) will fire.

    0 讨论(0)
提交回复
热议问题