Jquery Ajax在历史记录“onpopsate”上加载整个页面

Jquery Ajax在历史记录“onpopsate”上加载整个页面,jquery,browser-history,html5-history,Jquery,Browser History,Html5 History,我使用它来创建一个新状态: //state 1 history.pushState({ reload: true }, 'submodulo', '?submodulo=1'); //state2 history.pushState({ reload: true }, 'submodulo', '?submodulo=2'); //state3 history.pushState({ reload: true }, 'submodulo', '?s

我使用它来创建一个新状态:

//state 1
        history.pushState({ reload: true }, 'submodulo', '?submodulo=1');
//state2
        history.pushState({ reload: true }, 'submodulo', '?submodulo=2');
//state3
        history.pushState({ reload: true }, 'submodulo', '?submodulo=3');
我想在用户按下后退按钮时重新加载内容

window.onpopstate = function(event) {
          if (event.state !== null && event.state.reload) {

              location.reload(true);

            }               
    };

但是,这样,我将失去美国。这就像点击一个全新的url。它没有保持顺序。是否有其他方法可以加载整个页面,但要采用ajax方式?

您需要运行ajax调用,而不是在popstate事件上重新加载页面

window.onpopstate = function(event) {
   if (event.state !== null && event.state.reload) {
              $.ajax({
                url: 'load-content.php',
                success: function( response ) {
                     $('#main-content').replaceWith( response );      
                  }
              });
   }               
};

在目标PHP文件中,根据url或给定参数加载内容,并在成功后用生成的内容替换内容

各州仍然会犯错。例如,不能向后推两次。第一个很好,但第二个不行。或者先推后推。你的回答中有没有错误或提示?离题:在一些使用ajax导航的网站上,这是一个值得注意的问题。