How to Detect Browser Back Button event - Cross Browser

后端 未结 16 2762
鱼传尺愫
鱼传尺愫 2020-11-21 23:09

How do you definitively detect whether or not the user has pressed the back button in the browser?

How do you enforce the use of an in-page back button inside a sin

16条回答
  •  攒了一身酷
    2020-11-21 23:56

    I solved it by keeping track of the original event that triggered the hashchange (be it a swipe, a click or a wheel), so that the event wouldn't be mistaken for a simple landing-on-page, and using an additional flag in each of my event bindings. The browser won't set the flag again to false when hitting the back button:

    var evt = null,
    canGoBackToThePast = true;
    
    $('#next-slide').on('click touch', function(e) {
        evt = e;
        canGobackToThePast = false;
        // your logic (remember to set the 'canGoBackToThePast' flag back to 'true' at the end of it)
    }
    

提交回复
热议问题