2015-11-14 68 views
0

大家好我計劃用window.history.pushState在我的網站,但我有一點知識如何做到這一點..更新內容,更改URL,而無需加載window.history.pushState

我想要的頁面是,我有一個頁面dashboard.php並希望將網址更新爲dashboard.php?做=編輯&誰=我與負載ID contentdiv股利不加載整個頁面....我知道這可以通過此代碼完成

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

    window.onpopstate = function(e){ 
    if(e.state){ 
     document.getElementById("content").innerHTML = e.state.html; 
     document.title = e.state.pageTitle; 
    } 
}; 

但我不知道如何實現這個功能的迴應。

而且我想如果瀏覽器不支持此則在這種情況下,這應該像一個鏈接,並刷新頁面,可能是確定

任何想法/幫助表示讚賞

回答

1

使用一些JavaScript路由庫,例如Routie,你可以找到在GitHub - http://projects.jga.me/routie/

與路由和jQuery路由的示例中使用你下面有:

routie({ 
    'dashboard/edit/:who': function (who) { 
     $.ajax({ 
      url: 'api.php', 
      data: {prop: 'propVal'}, 
      method: 'POST' 
     }) 
     .done(function (res) { 
      console.log(res); 
     }); 
    } 
); 

要運行上述功能,請進入YOURSITE.COM#dashboard/edit/me而不是YOURSITE.COM/dashboard.php?=...

相關問題