简体   繁体   中英

submit form when user clicks browser back button

I have a situation here. I have a form which contains hidden value. When user clicks the browser back button then the form should be submitted. I have controlled the back space button but haven't found a solution for browser back button. Here is my code.

Form

 <form id="movevalue" action="/media" method="post">
    <input type="hidden" id="pagedetail" name="pagedetail" value="<?php echo $pagenumber; ?>" />
    <input type="submit" value="" style="display:none" />
 </form> 

At the end of page this is my script.

script

    <script>
    $(function(){  
      $(document).bind("keydown keypress", function(e){
      if( e.which == 8 ){ // 8 == backspace
        $('#movevalue').submit();
      }
     });
    });
   window.onbeforeunload = function(evt)    
   {    
    if (typeof evt == 'undefined')     
        $('#movevalue').submit() 
    }

   </script>

window.onbeforeunload this not work for me.

Just use this code and this will cover all state of page leave or close...

<script>
window.onbeforeunload = function() {
    $('#movevalue').submit();
   return true;
};
</script>

try this:

$(function () {
if (window.history && window.history.pushState) {
    window.history.pushState('', null, '');
    $(window).on('popstate', function (id) {
        $('#movevalue').submit();
    });
}

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM