Ajax加载页面在刷新后不更改加载的内容

时间:2012-10-16 16:56:36

标签: javascript jquery

情况是我有链接:

修改帐户信息 - 指向http://example.com/user/edit

我设置了一个javascript,它只会获取控制器中的信息,然后将其加载到模板中。 我有这个代码执行事件处理

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

它做的是,它用模板替换div的内容, 所以我通过ajax调用从控制器(从模型)加载,模板和细节。问题是,当我尝试刷新页面时,内容将是默认值。是否有可能当我点击链接时,URL将变为类似:

http://example.com/user#edit

所以即使我刷新页面,通过ajax加载的内容也是一样的? 如果我删除了#edit,那么它将是默认值吗?

2 个答案:

答案 0 :(得分: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 :(得分: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轻松存储用户设置。

相关问题