可以使用pushState

发布于 2024-10-17 02:14:34 字数 1008 浏览 4 评论 0原文

有谁知道有一个库可以确定是否可以使用pushState?

我正在使用这个:

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

但我刚刚发现 Safari 5.0.2 中有一个错误,导致即使上述测试通过也无法工作: http://support.github.com/discussions/site/2263-line-links-broken

我认为可能还有其他陷阱,并且可能已经有人找到它们并将它们包裹起来,但我还没有找到任何东西。

编辑: @Crescent Fresh

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

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 更改 url 时,location.pathname 不会更改。这适用于其他浏览器和 Safari 版本。

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

I was using this:

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

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: http://support.github.com/discussions/site/2263-line-links-broken.

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.

Edit:
@Crescent Fresh

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);
}

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぃ弥猫深巷。 2024-10-24 02:14:34

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

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

因此,对您来说,一种简单的方法是:

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

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

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);

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.

所谓喜欢 2024-10-24 02:14:34

PushState 是 HTML5 History API 的一部分。您可以使用常规 JavaScript 来测试支持,如下所示:

if (typeof history.pushState !== "undefined") {
    // pushState is supported!
}

或者,您可以使用 Modernizr 库:

if (Modernizr.history) {
     // pushState is supported!
}

pushState is part of the HTML5 History API. You can test for support using regular JavaScript like so:

if (typeof history.pushState !== "undefined") {
    // pushState is supported!
}

Alternatively, you can use the Modernizr library:

if (Modernizr.history) {
     // pushState is supported!
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文