小编Nic*_*ick的帖子

回到使用ajax的pushState条目

我遇到以下情况的问题.

  1. 用户访问网站
  2. 用户单击使用history.pushState更新URL的链接
  3. 通过ajax加载的部分页面内容(使用jQuery)
  4. 用户单击加载新页面的常规链接
  5. 用户单击返回以返回pushState条目
  6. 该页面现在仅显示通过ajax检索的页面部分

我已经使用pushState为各种事情增加了一个网站,结果是一个令人震惊的响应式网络应用程序,但这个问题阻止我使用它.

以下是代码示例:

$('a').live('click', function(){
  history.pushState({}, '', this.href);
  popstate(this.href);
  return false;
});

popstate = function(url){
  $('#content').load(url);
}
window.onpopstate = function(event){
  popstate(window.location.href);
  event.preventDefault();
}
Run Code Online (Sandbox Code Playgroud)

笔记:

  • 在window.onpopstate函数中发出警报时,似乎在步骤5中未触发该事件.这是一个错误吗?
  • 只有在使用ajax时才会出现此问题.
  • 我不得不分开popstate函数,所以我可以在pushState之后调用它.有没有办法手动触发window.onpopstate事件?

ajax jquery pushstate

16
推荐指数
1
解决办法
5563
查看次数

标签 统计

ajax ×1

jquery ×1

pushstate ×1