2012-09-21 63 views
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被解雇ONLY当我打开一个新的浏览器窗口并从那里开始工作时。记录最初的前进和后退事件,但后续事件不记录。History.js不点火

任何想法为什么发生这种情况?

+0

我已经在Mac OSX的Chrome 23和Firefox 15上测试过了 – Maverick

回答

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; 
    } 

});