在Chrome中加载页面时弹出窗口 [英] Popstate on page's load in Chrome

查看:129
本文介绍了在Chrome中加载页面时弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的网络应用使用了历史记录API,并且有一个问题。
我使用Ajax调用来更新页面上的一些结果,并使用 history.pushState()来更新浏览器的位置栏,而无需重新加载页面。然后,当然,我使用 window.popstate 为了恢复以前的状态,当点击后退按钮。



问题是很好的,已知 - Chrome和Firefox以不同的方式处理popstate事件。虽然Firefox在第一次加载时不启动,但Chrome仍在运行。我希望拥有Firefox风格并且不会在加载时触发事件,因为它只是在加载时使用完全相同的结果更新结果。除了使用 History.js 之外,是否有解决方法?我不想使用它的原因是 - 它本身需要太多的JS库,因为我需要它在已经有太多JS的CMS中实现,所以我想尽量减少JS 。

因此,想知道是否有办法让Chrome在加载时不会触发'popstate',或者有人试图使用History.js作为所有库合并为一个文件。

解决方案

在版本19的Google Chrome中,@spliter的解决方案停止工作。正如@johnnymire指出的,Chrome 19中的history.state存在,但它是空的。



我的解决方法是添加 window.history.state!== null 检查window.history中是否存在状态:

  var popped =(window.history& amp; amp; amp; amp; ;& window.history.state!== null),initialURL = location.href; 

我在所有主流浏览器和Chrome 19和18版本中都测试过它。

I am using History API for my web app and have one issue. I do Ajax calls to update some results on the page and use history.pushState() in order to update the browser's location bar without page reload. Then, of course, I use window.popstate in order to restore previous state when back-button is clicked.

The problem is well-known — Chrome and Firefox treat that popstate event differently. While Firefox doesn't fire it up on the first load, Chrome does. I would like to have Firefox-style and not fire the event up on load since it just updates the results with exactly the same ones on load. Is there a workaround except using History.js? The reason I don't feel like using it is — it needs way too many JS libraries by itself and, since I need it to be implemented in a CMS with already too much JS, I would like to minimize JS I am putting in it.

So, would like to know whether there is a way to make Chrome not fire up 'popstate' on load or, maybe, somebody tried to use History.js as all libraries mashed up together into one file.

解决方案

In Google Chrome in version 19 the solution from @spliter stopped working. As @johnnymire pointed out, history.state in Chrome 19 exists, but it's null.

My workaround is to add window.history.state !== null into checking if state exists in window.history:

var popped = ('state' in window.history && window.history.state !== null), initialURL = location.href;

I tested it in all major browsers and in Chrome versions 19 and 18. It looks like it works.

这篇关于在Chrome中加载页面时弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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