How to Detect Browser Back Button event - Cross Browser

后端 未结 16 2706
鱼传尺愫
鱼传尺愫 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:50

    Here's my take at it. The assumption is, when the URL changes but there has no click within the document detected, it's a browser back (yes, or forward). A users click is reset after 2 seconds to make this work on pages that load content via Ajax:

    (function(window, $) {
      var anyClick, consoleLog, debug, delay;
      delay = function(sec, func) {
        return setTimeout(func, sec * 1000);
      };
      debug = true;
      anyClick = false;
      consoleLog = function(type, message) {
        if (debug) {
          return console[type](message);
        }
      };
      $(window.document).click(function() {
        anyClick = true;
        consoleLog("info", "clicked");
        return delay(2, function() {
          consoleLog("info", "reset click state");
          return anyClick = false;
        });
      });
      return window.addEventListener("popstate", function(e) {
        if (anyClick !== true) {
          consoleLog("info", "Back clicked");
          return window.dataLayer.push({
            event: 'analyticsEvent',
            eventCategory: 'test',
            eventAction: 'test'
          });
        }
      });
    })(window, jQuery);
    
    0 讨论(0)
  • 2020-11-21 23:53
    if (window.performance && window.performance.navigation.type == window.performance.navigation.TYPE_BACK_FORWARD) {
      alert('hello world');
    }
    

    This is the only one solution that worked for me (it's not a onepage website). It's working with Chrome, Firefox and Safari.

    0 讨论(0)
  • 2020-11-21 23:53

    This will definitely work (For detecting back button click)

    $(window).on('popstate', function(event) {
     alert("pop");
    });
    
    0 讨论(0)
  • 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)
    }
    
    0 讨论(0)
  • 2020-11-21 23:58

    I had been struggling with this requirement for quite a while and took some of the solutions above to implement it. However, I stumbled upon an observation and it seems to work across Chrome, Firefox and Safari browsers + Android and iPhone

    On page load:

    window.history.pushState({page: 1}, "", "");
    
    window.onpopstate = function(event) {
    
      // "event" object seems to contain value only when the back button is clicked
      // and if the pop state event fires due to clicks on a button
      // or a link it comes up as "undefined" 
    
      if(event){
        // Code to handle back button or prevent from navigation
      }
      else{
        // Continue user action through link or button
      }
    }
    

    Let me know if this helps. If am missing something, I will be happy to understand.

    0 讨论(0)
  • 2020-11-22 00:05

    See this:

    history.pushState(null, null, location.href);
        window.onpopstate = function () {
            history.go(1);
        };
    

    it works fine...

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