Pages

Friday 15 April 2011

How to warn the user that all unsaved changes will be lost when he is leaving the page Part 3

Here is some progress on my jQuery plugin for warning a user when he is leaving the page and there are unsaved changes.

    1 // Version 0.2

    2 // April 10 2011 22:54

    3 // Julian Vargas

    4 // http://gr8code.blogspot.com

    5 

    6 (function () {

    7     var userIsEditing = false;

    8     var message = "Do you really want to abandon this page?, unsaved changes will be lost.";

    9     var controlsToObserve = ":input, :text, :password, :radio, :checkbox, select";

   10     var eventsToObserve = "keyup change";

   11 

   12     // the function is declared as a 'var' for DRY.

   13     var confirmation = function (event) {

   14         if (userIsEditing && !confirm(message))

   15             event.preventDefault();

   16     }

   17 

   18     $("a").click(confirmation);

   19     $(window).unload(confirmation);// This still needs improvements

   20 

   21     $(controlsToObserve).bind(eventsToObserve, function () { userIsEditing = true; });

   22 

   23 })();

No comments:

Post a Comment