2
    $(function(){

        // Prepare
        var History = window.History; // Note: We are using a capital H instead of a lower h
        if ( !History.enabled ) {
             // History.js is disabled for this browser.
             // This is because we can optionally choose to support HTML4 browsers or not.
            return false;
        }

        // Bind to StateChange Event
        History.Adapter.bind(window,'statechange',function(){ // Note: We are using statechange instead of popstate
            var State = History.getState(); // Note: We are using History.getState() instead of event.state
            History.log(State.data, State.title, State.url);
        });
});

我正在尝试使用 history.js 作为跟踪状态更改(前进和后退按钮)的一种方式,并且似乎只有当我打开一个新的浏览器窗口并从那里工作时才会触发 window.statechange。记录最初的前进和后退事件,但不记录后续事件。

任何想法为什么会这样?

4

1 回答 1

0

我意识到这个问题很老了。解决方案是将 statechange 函数移到 body load 函数之外:

// Bind to StateChange Event
History.Adapter.bind(window,'statechange',function(){ // Note: We are using statechange instead of popstate
    var State = History.getState(); // Note: We are using History.getState() instead of event.state
    History.log(State.data, State.title, State.url);
});

$(function(){

    // Prepare
    var History = window.History; // Note: We are using a capital H instead of a lower h
    if ( !History.enabled ) {
         // History.js is disabled for this browser.
         // This is because we can optionally choose to support HTML4 browsers or not.
        return false;
    }

});
于 2014-03-27T04:00:28.833 回答