2012-10-16 24 views
1

的情況後加載的內容是我有鏈接:阿賈克斯加載不改變刷新

編輯帳戶信息 - 這點http://example.com/user/edit

我安裝一個JavaScript只會在控制器中獲取信息然後將其加載到模板中。 我有這樣的代碼,做了eventhandling

$("#lnkEditUser").on("click", 
    function() 
    { 
    some.settings.edit_profile(); 
    return false; 
    }) 

它所做的,就是它取代了股利與模板的內容, 所以我把它裝,模板和控制器的細節(從模型)通過ajax調用。問題是,當我嘗試刷新頁面時,內容將是其默認值。是否有可能,當我點擊鏈接的URL會再更改爲類似:

http://example.com/user#edit 

所以,即使我刷新頁面,通過AJAX加載的將是相同的內容? 如果我刪除#edit,它會是默認的?

+2

你可以像這樣添加link點擊錨標記它會被追加到url – fuzionpro

+0

根據fuzionpro所說的擴展,你需要測試'url string'中發生的'hash'和然後執行邏輯來加載這些數據。就像'if(window.location.hash =='edit'){//做模板加載} else {//加載默認頁面}' – Ohgodwhy

回答

0

糾正我,如果我這樣做的權利,或者如果有更好的方式來做到這一點..

首先我檢查,如果哈希存在,那麼得到的哈希值,將其賦值給變量hash然後我刪除了哈希,也許我也可以做到這一點replace("#", "")。我意識到其他的東西,而我打字這樣就像我應該已經轉換它hash字符串賦值的權利。但無論如何,這是我做到的。

activeTemplateViaHash : function() { 
    if (window.location.hash) { 
     var hash = window.location.hash; 
     hash = hash.toString().substr(1, hash.toString().length-1); 
     var method = new Function('some.settings.' + hash + '()'); 
     method(); 
    } 
}, 
+1

請閱讀['apply()'](https:// developer。 mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/apply)和'call()' –

+0

非常感謝你teresko !!!! –

0

試着這樣做:

activeTemplateViaHash : function() { 
    if (window.location.hash) { 
     var hash = window.location.hash; 
     hash = hash.replace("#", ""); 
     var method = some.settings[hash]; //find the hash method 
     if(method) { //if the method exists then run it 
      method(); 
     } 
     else { 
      alert(hash + " function doesn't exist!"); 
     } 
    } 
}, 

但不是依靠這樣的哈希值,我會用localStorageCOOKIES輕鬆地存儲用戶設置。