使用pushState后,浏览器中的后退按钮无法正常工作(在Chrome中) [英] back button in browser not working properly after using pushState (in Chrome)

查看:556
本文介绍了使用pushState后,浏览器中的后退按钮无法正常工作(在Chrome中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JS响应中使用这种类型的行

  if(history&& history.pushState){ 
history.pushState(null,null,'<%= j home_path%>');
}

但是当我点击返回在浏览器中,我看到了响应的JS代码,而不是前一页。



有没有办法让后退按钮工作,请求最后一个网址的页面(在 home_path 之前)pushStated in?



更新



我发现这个 ..似乎其他人也有同样的问题.. History.js不修正它要么

所以要确认一些评论那里..这只发生在chrome



我认为



<我认为所有这一切的根源在于popstate请求相同类型的pushstate docuemtn(js响应)。需要做的是在popstate请求html响应。我只是不知道如何

更多更新:



看到 http: //railscasts.com/episodes/246-ajax-history-state 它提及使用

  $(窗口).bind(popstate,function(){
$ .getScript(location.href);
});

解决了这个问题,但根据理解jQuery $ .getScript() withtout ajax缓存它将添加时间戳..污染了网址..关闭它使它不能再次使用相同的效果..



任何人都知道如何解决这个问题?



更多更新



我在整个interweb中追踪了问题的踪迹,并以 Chrome中的问题,它指向一个rails问题(然而firefox可以正常工作)和 rails-ujs中的问题有关在响应中不包含Vary Header的问题

可以找到 here

解决方案

我目前正在忙着做

  response.headers ['Vary'] ='接受'

好像解决了问题,一开始看。



如果任何人有同样的问题,请检查它并让我知道


I am using this type of line in an JS response

if (history && history.pushState){
     history.pushState(null, null, '<%=j home_path %>');
}

but when I click back in the browser, I see the JS code of the response instead of the previous page.

Is there a way to make the back button work so it would re-request the page of the last url (before home_path as pushStated in?

Update:

I've found this.. it seems other have the same issue with.. History.js doesn't fix it either

So to confirm some of the comments there.. This happens only in chrome

What I think

I think the root of all this is that the popstate requests the same type of of the pushstate docuemtn (js response). What needs to be done is on popstate request the html response.. I just don't know how

More updates:

seeing http://railscasts.com/episodes/246-ajax-history-state it mentions to use

 $(window).bind("popstate", function() {
      $.getScript(location.href);
    });

this solves the issue but according to understanding jQuery $.getScript() withtout ajax caching it will add timestamps.. which pollutes the url.. turning it off makes it not work again with the same effect..

Anyone knows how to solves this?

Even more Updates

I tracked the trails of the issue all over the interweb and ended with an issue in chrome which points to a rails issue (firefox works fine however) and an issue in rails-ujs concerning not including Vary Header in the responses

Example can be found here

解决方案

I am currently playing around with doing

 response.headers['Vary'] = 'Accept'

which seems to solve the problem, at first look.

If anyone has the same issue, please check it and let me know

这篇关于使用pushState后,浏览器中的后退按钮无法正常工作(在Chrome中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆