2014-07-06 72 views
3

我知道有很多与Chrome和popstate事件相关的主题,但是在将Chrome升级到版本35后,我遇到了一个新的问题。点击后退按钮后Chrome 35触发popstate

这种情况:你选择

我有类别和过滤器的列表页面,之后类别,网页等内容,通过AJAX重新加载和一切历史API工作正常处理。

但是,当您转到列表中的项目的详细页面 - 标准请求时,整个页面将被重新加载 - 然后在浏览器上按下返回按钮,您可以看到一个列表页面,您可以在上次点击某个项目后触发popstate (我不会在这里讨论关于初始页面加载时的chrome popstate,因为在版本34中popstate不会在init中触发),并且由于前面提到处理类别的代码会重新加载整个页面。

所以问题是,铬35触发popstate情况下,我们按后退按钮后,我的问题是:

如何检测该事件被触发的用户来了以后回来从详细信息页面到我的房源页面不重新加载这一个。

我尝试使用文档对象引用等等,但当它看起来像第一次解决方案时总是有这种情况,当它不工作。

回答

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”回调将跳过铬的初始onpopstate。

相关问题