How to Detect Browser Back Button event - Cross Browser

后端 未结 16 2726
鱼传尺愫
鱼传尺愫 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-22 00:05

    Browser: https://jsfiddle.net/Limitlessisa/axt1Lqoz/

    For mobile control: https://jsfiddle.net/Limitlessisa/axt1Lqoz/show/

    $(document).ready(function() {
      $('body').on('click touch', '#share', function(e) {
        $('.share').fadeIn();
      });
    });
    
    // geri butonunu yakalama
    window.onhashchange = function(e) {
      var oldURL = e.oldURL.split('#')[1];
      var newURL = e.newURL.split('#')[1];
    
      if (oldURL == 'share') {
        $('.share').fadeOut();
        e.preventDefault();
        return false;
      }
      //console.log('old:'+oldURL+' new:'+newURL);
    }
    .share{position:fixed; display:none; top:0; left:0; width:100%; height:100%; background:rgba(0,0,0,.8); color:white; padding:20px;
    
    
    
    
        Back Button Example
        
    
    
    
    
        Share
        
    
    
    

提交回复
热议问题