How to call an event when browser back button is clicked

后端 未结 2 1705
长情又很酷
长情又很酷 2021-01-13 04:38

How to call a jquery event when browser back button is clicked. I am using a single page application in asp.net mvc and i want to show a confirm box to leave the screen when

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-13 05:01

    jQuery(document).ready(function($) {
    
      if (window.history && window.history.pushState) {
    
        window.history.pushState('forward', null, './#forward');
    
        $(window).on('popstate', function() {
          alert('Back button was pressed.');
        });
    
      }
    });
    

    JavaScript or jQuery browser back button click detector

提交回复
热议问题