Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

javascript - Can use pushState

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...