Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent onbeforeunload from being called when clicking submit button

How can I prevent onbeforeunload from being called when clicking the submit button?

$(document).ready(function() 
 {
  $('input:text,input:checkbox,input:radio,textarea,select').one('change', function() 
  {
   //('BODY').attr('onbeforeunload',"return 'Leaving this page will cause any unsaved data to be lost.';");
   if(
   window.onbeforeunload = function() { return 'You have unsaved changes!'; }

  });

 });
like image 293
ASD Avatar asked Dec 29 '09 09:12

ASD


1 Answers

you can use .on() to bind onbeforeunload and then use .off() to unbind it in form submission, short and sweet

$(document).ready(function () {
    $(window).on('beforeunload', function(){
        return "You have unsaved changes!";
    });
    $(document).on("submit", "form", function(event){
        $(window).off('beforeunload');
    });
}
like image 156
Brent White Avatar answered Sep 22 '22 19:09

Brent White