【问题标题】:Ignore Safari's Initial Page Load popstate忽略 Safari 的初始页面加载弹出状态
【发布时间】:2015-04-11 00:53:55
【问题描述】:

我正在使用 pushState 并根据当前页面的 history.state 生成关于 popstate 事件的页面视图。

如果没有history.state我(希望)重新加载文档。

window.onpopstate = function(event) {
    if( window.history.state !== null ){
        // page was ajax created and history created by pushState
        // recreate with ajax
    }
    else{
        // page was loaded from server normally
        document.location.reload()
    }
}

问题在于 Safari 在初始页面加载时会触发 popstate 事件。 Chrome 和 Firefox 在后退/前进浏览器按钮上触发 popstate。

我想忽略 Safari 上的初始加载 popstate 事件(最好没有 setTimeout 并且没有浏览器检测)。

此外,该网站是混合的链接 - 一些会触发 pushState 和一些会从服务器正常加载 - 所以如果用户进入导航历史的十页并点击后退按钮,历史将有一个混合pushState 页面和非 pushState 页面。

【问题讨论】:

标签: javascript jquery safari


【解决方案1】:

我已经解决了将从window.history.state.order 获得的初始订单值保存在变量中的问题。当触发 on popstate 事件时,会将之前存储的值与当前订单值进行比较。如果它们相同,则无需执行任何操作(您必须忽略 Safari 触发的初始 popstate 值)。在这里你可以看到和示例:

var initial_oder = window.history.state.order || 0;    
window.onpopstate = function(event) {
    if( window.history.state !== null && window.history.state.order !== initial_order){
        // page was ajax created and history created by pushState
        // recreate with ajax
    }
    else{
        // page was loaded from server normally
        document.location.reload()
    }
}

希望对您有所帮助!

【讨论】:

  • 嘿 IllRepublica,感谢您的回答,我认为这让我非常接近。我只是不确定你从哪里拉window.history.state.order?是来自history.pushState吗?
  • IllRepublica 正在他们自己的应用程序上定义它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-07
  • 2010-10-30
  • 2017-01-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多