在Chrome中加载页面时弹出窗口

编程入门 行业动态 更新时间:2024-10-26 10:33:03
本文介绍了在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中加载页面时弹出窗口

本文发布于:2023-11-27 22:57:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639956.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:弹出窗口   加载   页面   Chrome

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!