How can I detect the back/forwards buttons being clicked when using History.js?

前端 未结 3 1406
夕颜
夕颜 2020-12-19 06:07

Im using History.js (click) with jQuery in my code.

When loading new content via ajax i use:

History.pushState({my:stateobject},\"newtitle\",\"supern         


        
相关标签:
3条回答
  • 2020-12-19 06:34

    You wouldn't capture the back click event, as there is none.

    What you want to do is make sure the history object is in the right state by using window.history.pushState, window.history.popState, window.history.replaceState methods.

    https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history

    0 讨论(0)
  • 2020-12-19 06:37

    You need to bind an event handler to the statechange event, like this:

    $(window).bind('statechange',function(){
    // Do something, inspect History.getState() to decide what
    })
    

    There is a complete javascript snippet that comes with History.js, which can be used to 'ajaxify' a site completely: https://github.com/browserstate/ajaxify

    Take a look there for more inspiration.

    0 讨论(0)
  • 2020-12-19 06:45

    I was able to use the "onPopState" event to trigger my custom AJAX code.

    window.onpopstate = function(event) {
      $(fellowModal).foundation('reveal', 'close');
    };
    

    I tried several other things and that is what worked for me.

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