11

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.

Nicole
  • 32,841
  • 11
  • 75
  • 101
keegan3d
  • 10,357
  • 9
  • 53
  • 77
  • What part of that linked page says testing for `window.history.pushState` is invalid? It seems the bug is related to something specific github is doing (`pushState` in conjunction with setting `location.hash`, from what I can gather). – Crescent Fresh Feb 11 '11 at 06:09

2 Answers2

24

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.

Mauvis Ledford
  • 40,827
  • 17
  • 81
  • 86
  • 1
    it should actually be (!!window.history && !!history.pushState)....(window.history && history.pushState) will itself return false/true...no point using !! outside it.. – bhavya_w Dec 18 '14 at 11:33
  • An alternative would be `window.history && 'pushState' in window.history`. – sn3p Feb 05 '16 at 13:14
16

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!
}
dbau
  • 16,009
  • 2
  • 21
  • 31