2014-09-29 95 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