Aby Design : Custom Web Design and Development

jQuery – Save Changes Before Leaving Page

I have to give credit to cjsilence for this – it’s incredible.

With this simple jQuery function, you can notify a user if they’ve made any un-saved changes to a form field – and give them a chance to save or discard those changes. I was recently able to try this out on a work project and it is working beautifully.

I have a feeling THIS will come in very handy for a variety of projects.

var isDirty = false;
var msg = 'You haven\'t saved your changes.';

$(document).ready(function(){
   $(':input').change(function(){
      if(!isDirty){
         isDirty = true;
      }
   });

   window.onbeforeunload = function(){
      if(isDirty) {
         return msg;
      }
   };
});

Simply Beautiful and Beautifully Simple.

Leave a Comment

Recently Delicious

Spreading the Word
© 2010 Aby Design. All Rights Reserved.
An company.