有没有更好的方法来跟踪标签的分页?

Is there a better way to track pagination with hashtags?

本文关键字:标签 分页 跟踪 更好 方法 有没有      更新时间:2023-09-26

使用hashchange事件,我检测用户何时单击浏览器中的后退按钮并相应地更改URL。对于分页,是否有更好的方法来执行此操作?我目前正在更改URL后,用户单击我的分页控件,如下所示:

$(".pager").click(function(){
    var start = null;
    if ($.browser.msie) {    
        start = $(this).attr('href').slice($(this).attr('href').indexOf('#')+1);
    } 
    else {    
        start = $(this).attr('href').substr(1);
    }
    $('#start').val(start);
    $.post("visits_results.php", $("#profile_form_id").serialize(),
    function(data) {
        $('#search_results').html(data);
        location.href = "#visits=" + start; 
    }); 
    return false;
});

我的javascript检测后退按钮看起来像这样:

function myHashChangeCallback(hash) {
    if (hash == "") {
        $("#loadImage").show();
        var no_cache = new Date().getTime();
        $('#main').load("home.php?cache=" + no_cache, function () { 
        $("#loadImage").hide();
    });
    return false;
    }
    else {
    // adding code to parse the hash URL and see what page I'm on...is there a better way?; 
    }
}
function hashCheck() {
    var hash = window.location.hash;
    if (hash != _hash) {
        _hash = hash;
        myHashChangeCallback(hash);
    }
}

我目前计划检查每个标签和值,看看我应该加载哪个页面,除非有一个更好的更有效的方式。

有什么想法或建议吗?

jQuery Address插件做得很好。设置完成后,它提供了一系列可以挂接的逻辑导航事件。它还对history.pushState()有很好的支持,这消除了在较新的浏览器中对标签的需要,并且对那些不支持pushState的浏览器也有同样好的回退支持。

一个简单的实现是这样的:

// Run some code on initial load
$.address.init(function(e) {
    // Address and path details can be found in the event object
    console.log(e);
});
// Handle hashtag/pushState change events
$.address.change(function(e) {
    // Do more fancy stuff. Don't forget about the event object.
    console.log(e);
});
// Setup jQuery address on some elements
$('a').address();

要启用pushState()支持,向脚本传递如下参数:

<script type="text/javascript" src="jquery.address-1.3.min.js?state=/absolute/path/to/your/application"></script>