可以使用pushState

编程入门 行业动态 更新时间:2024-10-20 03:28:16
本文介绍了可以使用pushState的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有没有人知道确定是否可以使用pushState的库?

Does anyone know of a library that determines if pushState can be used?

我使用的是:

if(window.history.pushState){ window.history.pushState(null, document.title, path); }else{ location.pathname = path; }

但我发现Safari 5.0.2中存在一个错误即使上述测试通过,它也无法工作: support.github/ discussion / site / 2263-line-links-broken 。

But I just found out that there is a bug in Safari 5.0.2 that causes it not to work even though the above test passes: support.github/discussions/site/2263-line-links-broken.

我想可能还有其他陷阱,而且有人可能已经找到了它们并且包裹了em我还没有找到任何东西。

I'm thinking there might be other gotchas and someone has probably already found them and wrapped em up but I haven't found anything yet.

编辑: @Crescent Fresh

@Crescent Fresh

从我看到的情况来看,似乎pushState推送到历史堆栈并更改了url但不更新location.pathname。在我的代码中,我使用setInterval来检查路径是否已更新。

From what I've seen it seems like pushState pushes onto the history stack and changes the url but doesn't update location.pathname. In my code I'm using setInterval to check if the path has updated.

var cachedPathname = location.pathname; if(window.history.pushState){ cachedPathname = location.pathname; setInterval(function(){ if(cachedPathname !== location.pathname){ cachedPathname = location.pathname; //do stuff } }, 100); }

在Safari 5.0.2中,当pushState更改时,location.pathname不会更改网址。这适用于其他浏览器和Safari版本。

In Safari 5.0.2 the location.pathname doesn't change when pushState changes the url. This works in other browsers and versions of Safari.

推荐答案

查看Modernizer源代码,这是检查推送状态的方式:

Looking at the Modernizer source code this is how it checks for push state:

tests['history'] = function() { return !!(window.history && history.pushState); };

所以一个简单的方法就是:

So a simple way for you would just be:

var hasPushstate = !!(window.history && history.pushState);

首先必须检查是否存在 window.history 在深入两个级别之前,这可能是您遇到错误的原因。

One must first check for the existence of window.history before going two levels deep and that's probably why you were experiencing an error.

更多推荐

可以使用pushState

本文发布于:2023-11-01 18:12:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550047.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可以使用   pushState

发布评论

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

>www.elefans.com

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