dionarap's avatar

Issue with browser buttons and AJax pagination

I am bringing back a set of results of Blog posts which are paginated and the pagination is controlled using AJAX to swap the pages. The problem i have is that when i click on a blog post and then click the browsers back button to return to the list of blog post results and the page i was looking at it returns to 'Page 1'. How do i rectify this issue to return to the page i was looking at before i entered the single blog post rather than it resetting to page 1....

here is my code:

JS:

             $('.container').on('click', '.pagination a', function(zmm) {
                 zmm.preventDefault();
                 var page = $(this).attr('href').split('page=')[1];
                 var sortby = $('#SortBy').val();

                 $.ajax({
                     url: '/searchsort?page='+page,
                     data: {SortbyList: sortby, name : name, city :city, country :country, category :category, sl:sl},
                     success: function(data){
                         $('.search-results-holder').html(data).load();
                         $('html,body').animate({
                             scrollTop: $(".profile").offset().top -100
                         });

                     }

                 })

             });
0 likes
6 replies
jlrdw's avatar

You have to build the proper click link to return. Don't use back button.

jlrdw's avatar
window.location.href = "site/whatever?page=5";  // 5 would be a variable
jlrdw's avatar

Watch this

https://drive.google.com/file/d/1qWttpTHSkOwwSeuZQAHHa4uQwKCMVVzl/view

Notice the back link, give me a couple of minutes. Will show code for link

Laravel has a:

return redirect()->back();

Or build one:

index page

        $areturn = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        $haystack = $areturn;
        $needle = '?';
        $pos = strripos($haystack, $needle);
        if ($pos === false) {
            $areturn = $areturn . "?p=1&t1=" . $t1;
            Session::put('areturn', $areturn);
        } else {

            Session::put('areturn', $areturn);
        }

On edit or view page:

<?php $vurl = Session::get('areturn'); ?>
<a href="<?php echo $vurl; ?>">back</a>  

Or if edit page is also brought up using JS use the

window.location.href ...
dionarap's avatar

@JLRDW - So you would create a back button and try to disable the browsers back button and forward buttons?

jlrdw's avatar

Using the browsers back in an application is never a good idea.

It's Okay if searching google, and simple hitting back.

Maybe you could use a modal.

Please or to participate in this conversation.