Chrome 35在点击后退按钮后触发popstate事件

3
我知道有很多与chrome和popstate事件相关的主题,但是在将chrome升级到35版本后,我遇到了一个新问题。
情况如下:
我有一个带有类别和过滤器的列表页面,在选择类别、页面等之后,通过ajax重新加载内容,一切都由历史API处理,这很好。
但是,当您进入列表中项目的详细页面时 - 标准请求,整个页面被重新加载 - 然后在浏览器上按下后退按钮,您可以看到最后单击项目的位置处的列表页面,但然后会触发popstate事件(我在此不谈论chrome在初始页面加载时的popstate,因为在34版本中,在init时不再触发popstate),并且由于之前提到的处理类别的代码,整个页面被重新加载。
因此,问题是chrome 35在我们按下后退按钮后触发popstate事件,我的问题是:
如何检测用户从详细页面回到我的列表页面后触发此事件,以便不重新加载此事件。
我尝试使用文档对象引用等,但当它第一次看起来像解决方案时,总是有一种不起作用的情况。
1个回答

0
我一直遇到这个问题。我的解决方案有点像黑客,但除此之外似乎还不错。我发现在设置我的onpopstate处理程序之前等待150毫秒足以等待Chrome运行其初始onpopstate处理程序,您可以利用它来获得“优势”。例如,检测是否运行了初始的onpopstate:
// wait for page to load
$(function() {
  var no_initial_onpopstate = true;
  var enabled = true;

  window.onpopstate = function() {
    if(!enabled) return;
    console.log("detected initial onpopstate");
    no_initial_onpopstate = false;
  }

  setTimeout(function() {
    enabled = false;
    console.log("No initial onpopstate: ", no_initial_onpopstate);

    // no_initial_onpopstate indicates if the browser emitted an initial
    // onpopstate event. Do with that information what you will

    // 150ms delay seems to be the "magic number" to wait for Chrome to
    // run all of its initial onpopstate handlers (in my tests on localhost, at least)
  }, 150);
});

所以,从这个中我了解到,在浏览器的DOM准备就绪事件后150ms设置`onpopstate'回调函数将跳过Chrome的初始onpopstate。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接