onpopstate handler - ajax back button

前端 未结 3 1366

I\'m using pushState to create meaningful urls for ajax content on my site. I want to refresh the ajax content when the user hits the forward or back buttons. I\'m having pr

相关标签:
3条回答
  • 2020-12-11 04:25

    The object passed to the onpopstate function will have a state member, which will be null on the page load, so you could do something like this:

    window.onpopstate = function(event) {
      if(event && event.state) {
        // ...
      }
    }
    
    0 讨论(0)
  • 2020-12-11 04:28

    One way would be to initialize a variable to false on page load, and then on your popstate event, check that variable. If it's still false, set it to true and do nothing. From then on all of your other popstate events should be only from back/forward events.

    var loaded = false;
    
    window.onpopstate = function(e) {
        if (!loaded) {
            loaded = true;
            return;
        } else {
            // run your stuff...
        }
    }
    
    0 讨论(0)
  • 2020-12-11 04:33

    //example: http://www.nseri.com/news/5536

    window.onpopstate = function(e) {
         var count = String(location.pathname).split('/').length;
         if (count<4) {
              location.href = location.href;
         }else {
              $.ajaxLoad(location.pathname);
         }
    }
    
    0 讨论(0)
提交回复
热议问题