Javascript history.js-初始页面未存储在历史记录中

Javascript history.js-初始页面未存储在历史记录中,javascript,jquery,html,Javascript,Jquery,Html,我正在使用history.js在AJAX产品页面上启用向后/向前浏览器导航。AJAX功能基于单击产品类别 我有这个工作相当好-除了有一个问题,我正在经历。第一页没有存储在历史记录中。这是我的密码: HTML: 现在假设我加载URL://mysite/category/10 然后我点击类别5,我得到URL://mysite/Category/5 然后单击后退按钮-URL不会更改,AJAX函数会返回不正确的结果集 我做了一个console.log,它告诉我“State.data.category”是

我正在使用history.js在AJAX产品页面上启用向后/向前浏览器导航。AJAX功能基于单击产品类别

我有这个工作相当好-除了有一个问题,我正在经历。第一页没有存储在历史记录中。这是我的密码:

HTML:

现在假设我加载URL://mysite/category/10

然后我点击类别5,我得到URL://mysite/Category/5

然后单击后退按钮-URL不会更改,AJAX函数会返回不正确的结果集

我做了一个console.log,它告诉我“State.data.category”是未定义的

看起来初始页面没有存储在历史会话中,我已尝试在页面加载时强制执行history.pushState,但这意味着在页面加载时也会触发“statechange”事件,这是不必要的

有人知道如何解决这个问题吗?

您可以通过jQuery将statechange事件调用到:

<ul class="category">
    <li><a href="/category/5">Category 5</a></li>
    <li><a href="/category/10">Category 10</a></li>
</ul>
$(document).ready(function(){

    var
        History = window.History,
        State = History.getState();

    $('.category a').click(function(){

        var path = $(this).attr('href');
        var title = $(this).next().text();
        var category = $(this).attr('data-cat');

        History.pushState({category: category}, title, path);

        return false;
    });

});


History.Adapter.bind(window, 'statechange', function() {
    var State = History.getState();
    var category = State.data.category;

    myAjaxFunction(category); // function which refreshes the product container
});
$(document).ready(function(){ 

    ...

    jQuery(window).trigger('statechange');

});