【问题标题】:history.js - initial page not being stored in historyhistory.js - 初始页面未存储在历史记录中
【发布时间】:2013-07-11 20:50:51
【问题描述】:

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

我的这个工作很好 - 除了我遇到一个问题。第一页没有存储在历史记录中。这是我的代码:

HTML:

<ul class="category">
    <li><a href="/category/5">Category 5</a></li>
    <li><a href="/category/10">Category 10</a></li>
</ul>

JS:

$(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
});

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

然后我点击类别 5 - 我得到 URL://mysite/category/5

然后我点击返回按钮 - URL 没有改变,AJAX 函数带回不正确的结果集

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

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

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

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

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

    $(document).ready(function(){ 
    
        ...
    
        jQuery(window).trigger('statechange');
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-10
      • 2022-11-03
      • 1970-01-01
      相关资源
      最近更新 更多