princewillopah's avatar

Chrome Back button is not preserving the url history when using Ajax for pagination

Hey guys, pls help me out. i recent used ajax for my laravel pagination. Whenever i click he back button. it does not go back to the page i am coming from. it goes back to page 1. let me Illustrate this clearly. (1) i have a list of items paginated with jquery ajax. (2) when i visit the 5th page( for example) and click an item to view details of that item and decided to click the browser back button, i am expected to be taken back to page 5 where i was. instead, i am taken back to page 1. (3) This is only happening in chrome related browsers. it works well in Firefox;

//BELOW IS MY AJAX CODE;

$(document).ready(function(){

$(document).on('click','.pagination a',function(e){
        e.preventDefault();
       var pageNumber = $(this).attr('href').split('page=')[1];
       getMorePage(pageNumber)
  });

  function getMorePage(page) {
    $.ajax({
        url: "/all-products-ajax/"+"?page="+page,
        type: "get",
        datatype: "html"
    }).done(function (data) {
        $('#product-view').html(data); 
    }).fail(function () {
        alert('Posts could not be loaded.');
    });
}

});

0 likes
3 replies
jlrdw's avatar

Make a window.onhashchange function and work out whatever you need within the function.

princewillopah's avatar

thank you for your reply. however, i am new to this. can you pls illustrate with a code snippet. pls

Hoa Thanh's avatar

You can save param in Local Storage JavaScript, after when you back url, you check get param in local storage, call function ajax

Please or to participate in this conversation.