Sunday, November 29, 2015

Confirm message before close


If the user unknowingly closes the application. He might loose important information.

Solution
We can avoid this by providing confirm message before close. The user would have the option to save his work or to close the application

The event which we can use is window.onbeforeunload 


Cut and past this code in your javascript file
 //Confirmation message before leaving the page
        window.onbeforeunload = function(e) {
            e = e || window.event;
             // For IE and Firefox prior to version 4
            if (e) {
                e.returnValue = 'Please save your work.';
            }
            // For Safari
            return 'Please save your work.';
        };



Note : 

  • This would work for all browsers
  • We can also have  a flag to skip confirmation. If the flag is set we would not show the message. 

Output

No comments:

Post a Comment