2014-09-29 79 views
0

一整页我用它来创建一个新的状态:阿贾克斯加载历史“onpopsate”

//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); 

      }    
    }; 

但是,这样我就会失去状态。这就像点击一个完整的新网址。它不保留序列。有没有另一种方式来加载整个页面,但以Ajax方式?

回答

0

您需要运行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或给定参数加载内容,并在成功时用生成的内容替换内容。

+0

各州仍然错误。例如,不能推回2次。第一个工作正常,但不是第二个。或推回,然后向前。 – Camilla 2014-09-29 23:28:12

+0

你的回复中是否有错误或提示? Offtopic:这是一些使用Ajax进行导航的网站。 – TheFrozenOne 2014-09-29 23:30:23

+0

不幸的是没有错误... – Camilla 2014-09-29 23:37:59