tgr*_*ass 7 javascript safari jquery
我正在使用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()
}
}
Run Code Online (Sandbox Code Playgroud)
问题是Safari在初始页面加载时触发popstate事件.Chrome和Firefox在后退/前进浏览器按钮上激活popstate.
我想忽略Safari上的初始加载popstate事件(理想情况下没有setTimeout且没有浏览器检测).
此外,该网站是一个混合链接 - 一些将触发pushState和一些将从服务器正常加载 - 所以如果用户是十页导航历史记录并单击后退按钮,历史将混合pushState页面和非pushState页面.
小智 5
我已经解决了保存从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()
}
}
Run Code Online (Sandbox Code Playgroud)
希望这有帮助!