window bind POPSTATE

前端 未结 8 1984
一整个雨季
一整个雨季 2020-12-07 21:25

Given the following:

$(window).bind(\"popstate\", function() {
    alert(\'popstate\');
});

On first load, the alert fires with FireFox and

8条回答
  •  天命终不由人
    2020-12-07 21:59

    See the code from pjax. pjax is fairly popular open source library now, so the below logic might be the best to avoid this issue.

    var popped = ('state' in window.history), initialURL = location.href
    $(window).bind('popstate', function(event) {
      // Ignore inital popstate that some browsers fire on page load
      var initialPop = !popped && location.href == initialURL
      popped = true
      if ( initialPop ) return
      ...
    

    https://github.com/defunkt/jquery-pjax/blob/master/jquery.pjax.js

提交回复
热议问题