Can use pushState

前端 未结 2 1444
一个人的身影
一个人的身影 2020-12-15 09:59

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

I was using this:

if(window.history.pushState){
    window.history.pushState(         


        
相关标签:
2条回答
  • 2020-12-15 10:13

    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!
    }
    
    0 讨论(0)
  • 2020-12-15 10:29

    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.

    0 讨论(0)
提交回复
热议问题