绑定窗口到popstate事件触发两次

Ric*_*ney 3 jquery html5 history

只需将窗口(在jQuery中)绑定到popstate,事件总是被触发两次.

$( window ).bind( 'popstate', myFunction );
Run Code Online (Sandbox Code Playgroud)

myFunction()中没有任何内容可以导致这种情况 - 我已经尝试将此函数剥离为一个简单的:

console.log( 'triggered' );
Run Code Online (Sandbox Code Playgroud)

结果是一样的.它总是被触发两次(在Safari,Chrome和Firefox中测试过).

我知道HTML5历史API存在问题,但是除了'尝试History.js'之外的任何建议都会感激不尽!

Ric*_*ney 6

好的,我找到了解决这个问题的方法:

jQuery( function( $ ){

  var currentPageNo = location.hash || 1;

  var myFunction = function(){

    var pageNo = location.hash;

    if( pageNo != currentPageNo ){

      // trigger AJAX stuff here

      currentPageNo = pageNo;

    }

  };

  $( window ).bind( 'popstate', myFunction );

});
Run Code Online (Sandbox Code Playgroud)

仅触发一次该功能,但状态仍然被触发两次!