首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可以使用pushState

可以使用pushState
EN

Stack Overflow用户
提问于 2011-02-11 06:04:49
回答 2查看 10.3K关注 0票数 11

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

我在用这个:

代码语言:javascript
复制
if(window.history.pushState){
    window.history.pushState(null, document.title, path);
}else{
    location.pathname = path;
}

但是我刚刚发现Safari5.0.2中有一个bug,即使上面的测试通过了:http://support.github.com/discussions/site/2263-line-links-broken,它还是不能工作。

我想可能还有其他的问题,可能有人已经找到并包装好了,但我还没有找到任何东西。

编辑:@新月

据我所见,pushState似乎推动了历史堆栈并更改了url,但没有更新location.pathname。在我的代码中,我使用setInterval检查路径是否已更新。

代码语言:javascript
复制
var cachedPathname = location.pathname;
if(window.history.pushState){
    cachedPathname = location.pathname;
    setInterval(function(){
        if(cachedPathname !== location.pathname){
            cachedPathname = location.pathname;
            //do stuff
        }
    }, 100);
}

在Safari5.0.2中,当location.pathname更改url时,pushState不会改变。这适用于其他浏览器和Safari版本。

EN

回答 2

Stack Overflow用户

发布于 2012-05-18 05:56:54

看看现代的源代码,这就是它如何检查推送状态:

代码语言:javascript
复制
  tests['history'] = function() {
      return !!(window.history && history.pushState);
  };

所以对你来说,一个简单的方法就是:

代码语言:javascript
复制
var hasPushstate = !!(window.history && history.pushState);

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

票数 24
EN

Stack Overflow用户

发布于 2011-11-01 05:23:01

pushState是HTML5历史API的一部分。您可以使用常规JavaScript进行测试以获得支持,如下所示:

代码语言:javascript
复制
if (typeof history.pushState !== "undefined") {
    // pushState is supported!
}

或者,您可以使用现代派库:

代码语言:javascript
复制
if (Modernizr.history) {
     // pushState is supported!
}
票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4966090

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档