简体   繁体   中英

Back Button not work when load page using history.pushState in JS

I am trying to load a page via ajax with history.pushState and page load successfully, but the back button does not work in this case. Please check my code below:

function processAjaxData(response, urlPath){
    document.write(response)
    document.title = '';
    window.history.pushState({"html":response,"pageTitle":''},"", urlPath);
}

$('#apply_loader').click(function(e){
   
      var nextPage = $(this).attr('href');
      $.get(nextPage,  // url
      function (data, textStatus, jqXHR) {  // success callback
        processAjaxData(data,nextPage);
      }
    );
});

I am trying to use popstate below to identify a back button click.

  $(window).on('popstate', function(event) {
    console.log('asd');
     location.reload();
  });

Maybe too late, but perhaps this will fix the problem:

Instead of location.reload(), try:

window.location = location.href

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