可以使用pushState [英] Can use pushState

查看:98
本文介绍了可以使用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中存在一个错误即使上述测试通过,它也无法工作: http://support.github.com/ 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: http://support.github.com/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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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