How to Detect Browser Back Button event - Cross Browser

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

    The document.mouseover does not work for IE and FireFox. However I have tried this :

    $(document).ready(function () {
      setInterval(function () {
        var $sample = $("body");
        if ($sample.is(":hover")) {
          window.innerDocClick = true;
        } else {
          window.innerDocClick = false;
        }
      });
    
    });
    
    window.onhashchange = function () {
      if (window.innerDocClick) {
        //Your own in-page mechanism triggered the hash change
      } else {
        //Browser back or forward button was pressed
      }
    };
    

    This works for Chrome and IE and not FireFox. Still working to get FireFox right. Any easy way on detecting Browser back/forward button click are welcome, not particularly in JQuery but also AngularJS or plain Javascript.

提交回复
热议问题