3

因此,我正在使用 url 哈希来帮助我将特定内容带到hashchange. 现在,当我在某些特定内容上并刷新整个页面时,我在 url 中有那个哈希值,但仍然没有得到那个哈希值指示的内容。

说我的网址是: http://localhost/user我点击了详细信息,它把我的网址变成了以下: http://localhost/user#!details我有详细信息页面。

但是当我重新加载页面时,将上述内容作为我的 url,哈希值保持不变,哈希值没有变化,它不会调用我与 hashchange 事件绑定的函数。

此外,每次用户使用beforeunload.

有什么建议么?

我的代码:

var pages = {};
pages['home'] = ""; pages['details'] = ""; pages['reviews'] = ""; pages['favs'] = "";
pages['likes'] = "", pages['gallery'] = "", pages['visited'] = "", pages['coupons'] = "";

$(window).bind('hashchange', function(event) {
    var curr_hash = event.target.location.hash;
    var page_to_load = curr_hash.substr(2);
    var loadurl = "/ci_theyaw/user/user_" + page_to_load;
    load_page(loadurl, page_to_load);
});

function load_page(page, toload){

    // alert(pages[toload]);

    if(pages[toload] == "") {
        var post_data = {
            '<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash(); ?>'
        };

        $.ajax({
            type: 'POST',
            url: page,
            data: post_data,
            dataType: "html",
            success : function(data) {
                page[toload] = data;
                $('.right-pane').html(data);
            },
            error : function(xhr, ajaxOptions, thrownError){
                alert(xhr.status);
                console.log(xhr.responseText);
                alert(thrownError);
            }
        });
    }

    else {
        $('.right-pane').html(page[toload]);
    }
}
4

2 回答 2

5

您需要hashchange在加载时运行附加的代码,以防有人刷新页面,甚至直接将其键入浏览器。尝试这个:

var hashUpdate = function(curr_hash) {
    var page_to_load = curr_hash.substr(2);
    var loadurl = "/ci_theyaw/user/user_" + page_to_load;
    load_page(loadurl, page_to_load);
}

$(window).bind('hashchange', function(e) {
    hashUpdate(e.target.location.hash);
}); 
window.location.hash && hashUpdate(window.location.hash); // onload, if there was a fragment in the URL
于 2013-10-31T12:22:48.917 回答
0
$(function(){
    var curr_hash = window.location.hash;
    var page_to_load = curr_hash.substr(2);
    var loadurl = "/ci_theyaw/user/user_" + page_to_load;
    load_page(loadurl, page_to_load);
});
于 2013-10-31T12:20:13.957 回答