简体   繁体   English

刷新后页面加载回索引页面

[英]Page loads back to index page after refresh

I have created a website with multiple pages and pagination but when I am on page2 and refresh, It goes back to the index.php page. 我创建了一个包含多个页面和分页的网站,但是当我在page2并刷新时,它会返回到index.php页面。 Is there something to do with session storage or cookies in this case. 在这种情况下,是否与session storagecookies有关。 Any solution so that it will stay on the same page even after refreshing it. 任何解决方案,即使刷新后它也将保留在同一页面上。

Edit: 编辑:

so this is the pagination script and all other content is also in the same page 所以这是分页脚本,所有其他内容也在同一页面中

 <script type="text/javascript">
$(document).ready(function(){

    var show_per_page = 9; 

    var number_of_items = $('#content').children().size();

    var number_of_pages = Math.ceil(number_of_items/show_per_page);


    $('#current_page').val(0);
    $('#show_per_page').val(show_per_page);


    var navigation_html = '<a class="previous_link" href="javascript:previous();">Prev</a>';
    var current_link = 0;
    while(number_of_pages > current_link){
        navigation_html += '<a class="page_link" href="javascript:go_to_page(' + current_link +')" longdesc="' + current_link +'">'+ (current_link + 1) +'</a>';
        current_link++;
    }
    navigation_html += '<a class="next_link" href="javascript:next();">Next</a>';

    $('#page_navigation').html(navigation_html);


    $('#page_navigation .index.php:first').addClass('active_page');


    $('#content').children().css('display', 'none');


    $('#content').children().slice(0, show_per_page).css('display', 'block');

});

function previous(){

    new_page = parseInt($('#current_page').val()) - 1;

    if($('.active_page').prev('.page_link').length==true){
        go_to_page(new_page);
    }

}

function next(){
    new_page = parseInt($('#current_page').val()) + 1;
    if($('.active_page').next('.page_link').length==true){
        go_to_page(new_page);
    }

}
function go_to_page(page_num){
    var show_per_page = parseInt($('#show_per_page').val());

    start_from = page_num * show_per_page;

    end_on = start_from + show_per_page;

    $('#content').children().css('display', 'none').slice(start_from, end_on).css('display', 'block');

    $('.page_link[longdesc=' + page_num +']').addClass('index.php').siblings('.page2.php').removeClass('active_page');
        $('#current_page').val(page_num);
}
  </script>

Whenever any page is changed, you can add new page link in local storage. 每当更改任何页面时,都可以在本地存储中添加新页面链接。

window.localStorage.setItem('page', 'home.php');

When you refresh page, you need to check if there is any value for page in local storage. 刷新页面时,需要检查本地存储中的页面是否有任何值。

var startPage = window.localStorage.getItem('page');

if (startPage) {
    window.location.replace(startPage);
}
so this is the pagination script  and all other content is also in the same page 

 <script type="text/javascript">
$(document).ready(function(){

    var show_per_page = 9; 

    var number_of_items = $('#content').children().size();

    var number_of_pages = Math.ceil(number_of_items/show_per_page);


    $('#current_page').val(0);
    $('#show_per_page').val(show_per_page);


    var navigation_html = '<a class="previous_link" href="javascript:previous();">Prev</a>';
    var current_link = 0;
    while(number_of_pages > current_link){
        navigation_html += '<a class="page_link" href="javascript:go_to_page(' + current_link +')" longdesc="' + current_link +'">'+ (current_link + 1) +'</a>';
        current_link++;
    }
    navigation_html += '<a class="next_link" href="javascript:next();">Next</a>';

    $('#page_navigation').html(navigation_html);


    $('#page_navigation .index.php:first').addClass('active_page');


    $('#content').children().css('display', 'none');


    $('#content').children().slice(0, show_per_page).css('display', 'block');

});

function previous(){

    new_page = parseInt($('#current_page').val()) - 1;

    if($('.active_page').prev('.page_link').length==true){
        go_to_page(new_page);
    }

}

function next(){
    new_page = parseInt($('#current_page').val()) + 1;
    if($('.active_page').next('.page_link').length==true){
        go_to_page(new_page);
    }

}
function go_to_page(page_num){
    var show_per_page = parseInt($('#show_per_page').val());

    start_from = page_num * show_per_page;

    end_on = start_from + show_per_page;

    $('#content').children().css('display', 'none').slice(start_from, end_on).css('display', 'block');

    $('.page_link[longdesc=' + page_num +']').addClass('index.php').siblings('.page2.php').removeClass('active_page');
        $('#current_page').val(page_num);
}
  </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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